簡體   English   中英

編寫一個程序來接受來自用戶的四位數字,並從輸入的數字中計算零、奇數和偶數

[英]Write A program to accept Four digit number from user and count zero , odd and even digits from the entered number

編寫一個程序來接受來自用戶的四位數字,並從輸入的數字中計算零、奇數和偶數。 請檢查我在哪里犯錯誤?

count_of_zero = 0
count_of_odd = 0
count_of_even = 0

n = int(input("Enter 4 digit number: "))

    for count in str(n):
        if count == 0:
            count_of_zero += 1
        elif count % 2 == 0:
            count_of_even += 1
        elif count % 2 != 0:
            count_of_odd += 1
    print(f"The count of Zero in {n} is {count_of_zero}, The count of Even digits in {n} is {count_of_even} & the count of Odd digits in {n} is {count_of_odd}")

將用戶輸入的值保留為字符串,直到您需要對其執行算術檢查。 下面我只將每個數字依次轉換為 int

count_of_zero = 0
count_of_odd = 0
count_of_even = 0

n = input("Enter 4 digit number: ")

for count in str(n):
    count = int(count)
    if count == 0:
        count_of_zero += 1
    elif count % 2 == 0:
        count_of_even += 1
    elif count % 2 != 0:
        count_of_odd += 1
print(f"The count of Zero in {n} is {count_of_zero}")
print(f"The count of Even digits in {n} is {count_of_even}")
print(f"& the count of Odd digits in {n} is {count_of_odd}")
count_zero = 0
count_odd = 0
count_even = 0
num = input("Enter a four digit number")
l=len(num)
if l != 4:
    print("This is not a 4 digit number")
else:
    for i in num:
        if int(i) == 0:
            count_zero= count_zero+1
        elif int(i)%2!=0:
            count_odd= count_odd+1
        elif int(i)%2==0:
            count_even=count_even+1
print('Number of zeroes', count_zero)
print('Number of odds', count_odd)
print('Number of even', count_even)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM