我参加Python课程,我们的教授希望我们编写一个程序,提示用户重复输入一个整数,直到输入0为止。然后,让程序忽略所有负数,如果有的话,显示偶数的整数,奇数的整数,偶数的和,奇数的和,以及正整数的数量。
我一直在尝试并尝试以小部分执行此程序。但是,我总是陷入困境。我现在已经开始了大约5次,如果有人指出我正确的方向,我真的很感激。
到目前为止,我有这个:
num_str = input("Input an integer (0 terminates):")
num_int=int(num_str)
even_count=0
odd_count=0
even_sum=0
odd_sum=0
while num_int !=0:
num_str = input("Input an integer (0 terminates):")
num_int=int(num_str)
for num_int in num_str:
if num_int%2 == 0:
even_count += 1
else:
odd_count +=1
print("")
print("Sum of odds:", odd_sum)
print("Sum of evens:", even_sum)
print("Even count:", even_count)
print("Odd count:", odd_count)
print("Total positive int count:")
我知道它并不多,而且我错过了很多,但我刚刚写了我知道需要包含的东西到目前为止。我一直卡住,因为程序一直给我错误。任何形式的帮助都非常感激,因为我真的不知道从哪里开始!
答案 0 :(得分:0)
要忽略负数,你可以将它们放在agian中,使用这样的if循环 if(num_str> 0): num_str = input(“那不是偶数,输入一个整数(0终止)”) 然后要添加它们,你必须像这样添加整数版本的num_str odd_sum + = int(num_str) 这里有一些代码供你试用
num_str = input("Input an integer (0 terminates):")
num_int=int(num_str)
even_count=0
odd_count=0
even_sum=0
odd_sum=0
total = even_count + odd_count
while num_int !=0:
num_str = input("Input an integer (0 terminates):")
num_int=int(num_str)
if num_int < 0:
num_str = input("Input an integer greater than 0.")
for num_int in num_str:
num_int = int(num_str)
if num_int % 2 == 0 and not num_int == 3 and not num_int == 0:
even_count += 1
even_sum = even_sum + num_int
elif not num_int == 0:
odd_count +=1
odd_sum = odd_sum + num_int
total = even_count + odd_count
print("")
print("Sum of odds:", odd_sum)
print("Sum of evens:", even_sum)
print("Even count:", even_count)
print("Odd count:", odd_count)
print("Total positive int count:", total)
答案 1 :(得分:0)
您的代码存在一些问题,但它们很小:
1)您在主循环之前要求输入数字,因此输入的第一个整数不会被求和(第1行和第2行)
2)你有一个像你主回路中那样的for循环没有意义。你正在做的是试图检查字符串中的每个字符。只是不是你想要的。
3)要忽略负数,只需检查它们是否小于0并继续(打破循环),如果它们是。
4)你使用3个空格的缩进。它可能是您的文本编辑器的错误,因此请尝试将其配置为使用4个空格,这是Python中的标准。
5)公约说运营商周围应该有一个空间。
6)正整数计数只是另一个简单的计数器。
所有修改过的,这就是你的代码应该是这样的:
num_int = None
even_count = 0
odd_count = 0
even_sum = 0
odd_sum = 0
while num_int != 0:
num_str = input("Input an integer (0 terminates):")
num_int = int(num_str)
if num_int < 0:
continue # Continue the loop and go back to asking input
# If the loop reaches this point we know it's a positive number, so just add one
positive_count += 1
if num_int % 2 == 0:
even_count += 1
even_sum += num_int
else:
odd_count +=1
odd_sum += num_int
print("")
print("Sum of odds:", odd_sum)
print("Sum of evens:", even_sum)
print("Even count:", even_count)
print("Odd count:", odd_count)
print("Total positive int count:", positive_count)
答案 2 :(得分:0)
你应该声明一个变量
total = 0
计算用户输入的整数数。
拥有
也更具可读性while True:
循环在输入为零时中断,而不是你拥有的循环。
在循环中,你应该
break
如果输入等于0,
continue
如果输入小于1,则增加even_count并在输入为偶数时添加到even_sum,
even_count += 1
even_sum += num
然后增加odd_count和odd_sum,
odd_count += 1
odd_sum += num
最后,你应该增加总数:
total += 1
还要确保将代码的最后一行更改为:
print("Total positive int count:", total)
显示总数
您的最终结果应如下所示:
even_count = 0
odd_count = 0
even_sum = 0
odd_sum = 0
total = 0
while True:
num = int(input("Input an integer (0 terminates): "))
if num == 0:
break
if num < 1:
continue
if num % 2 == 0:
even_count += 1
even_sum += num
else:
odd_count += 1
odd_sum += num
total += 1
print("")
print("Sum of odds:", odd_sum)
print("Sum of evens:", even_sum)
print("Even count:", even_count)
print("Odd count:", odd_count)
print("Total positive int count:", total)
答案 3 :(得分:0)
试试这个
userInput = None
oddSum = 0
oddCount = 0
evenSum = 0
evenCount = 0
while(userInput != 0):
userInput = int(input("Enter a number: "))
if(userInput > 0):
if(userInput % 2 == 0):
evenSum += userInput
evenCount += 1
elif(userInput % 2 != 0):
oddSum += userInput
oddCount += 1
print("even numbers: {} sum: {}".format(evenCount, evenSum))
print("odd numbers: {} sum: {}".format(oddCount, oddSum))
答案 4 :(得分:0)
val = []
inpt = None
evensm, oddsm = 0, 0
while inpt != 0:
inpt = int(input("Enter a number: "))
val.append(inpt)
for i in val:
if i % 2 == 0:
evensm += i
else:
oddsm += i
print("Sum of even integers is", evensm)
print("Sum of odd integers is", oddsm)
或者,如果您不想使用列表:
oddsm = 0
evensm = 0
while 1:
inpt = int(input("Enter a number: "))
if inpt == 0:
break
elif inpt % 2 == 0:
evensm += inpt
else:
oddsm += inpt
print("Sum of odd integers is", oddsm)
print("Sum of even integers is", evensm)
答案 5 :(得分:0)
Write a Python program to take input of positive numbers, with an appropriate prompt, from the user until the user enters a zero. Find total number of odd & even numbers entered and sum of odd and even numbers. Display total count of odd & even numbers and sum of odd & even numbers with appropriate titles.
sumOdd =0
sumEven = 0
cntOdd =0
cntEven = 0
while True :
no = int(input("enter a number (0 for exit)"))
if no < 0 :
print("enter positive no......")
elif no == 0 :
break
elif no % 2 == 0 :
cntEven = cntEven+1
sumEven = sumEven + no
else :
cntOdd = cntOdd+1
sumOdd = sumOdd + no
print ("count odd == ",cntOdd)
print ("sum odd == ",sumOdd)
print ("count even == ",cntEven)
print ("sum even == ",sumEven)
答案 6 :(得分:0)
Write a Python program to take input of a positive number, say N, with an appropriate prompt, from the user. The user should be prompted again to enter the number until the user enters a positive number. Find the sum of first N odd numbers and first N even numbers. Display both the sums with appropriate titles.
n = int(input("enter n no.... : "))
sumOdd =0
sumEven = 0
for i in range (n) :
no = int(input("enter a positive number : "))
if no > 0 :
if no % 2 == 0 :
sumEven = sumEven + no
else :
sumOdd = sumOdd + no
else :
print("exit.....")
break
print ("sum odd == ",sumOdd)
print ("sum even == ",sumEven)