我正在嘗試以非負整數的形式收集用戶輸入。然後我想取這個整數並告訴用戶它們的整數中有多少奇數,偶數和零個單獨的數字。如何從字符串中讀取單個數字並對其執行功能?
Ex。用戶輸入「123」,程序輸出Evens:1賠率:2個零:0
這是我的代碼到目前爲止。
def main():
print("1. Enter a new number")
print("2. Print the number of odd, even and zero digits in the integer")
print("3. Print the sum of the digits of the integer")
print("4. Quit the program")
value = (input("Please enter a non-negative integer"))
Sum = 0
evens = 0
odds = 0
zeros = 0
loop=True
while loop:
main()
choice = int(input("Enter a number between 1 and 4:"))
if choice==1:
loop=False
value = int(input("Please enter a non-negative integer"))
loop=True
elif choice==2:
loop=False
value_string = str(value)
for ch in value_string:
print(ch)
for [1] in value:
if i % 2 == 0:
evens = evens + 1
print(evens)
elif choice==3:
loop=False
while (value >0):
remainder = value % 10
Sum = Sum + remainder
value = value //10
print("Sum of the digits = %d" %Sum)
'對於[1]值:'有相當多的問題。 –
請修復您的縮進 –
再次放置代碼並使用按鈕「{}」在SO上正確格式化代碼。 – furas