【发布时间】:2021-11-29 01:11:42
【问题描述】:
我目前正在尝试制作一个程序,它首先询问用户可数整数的数量。然后程序继续向用户单独询问每个可数整数,最后,程序打印计算出的总整数的总和。 这是我的程序的代码:
amount_of_integers = int(input("How many numbers do you want to count together: "))
sum = 0
repeat_counter = 1
while repeat_counter <= amount_of_integers:
countable_integer = int(input(f"Enter the value of the number {repeat_counter}: "))
sum += countable_integer
repeat_counter += 1
print()
print("The sum of counted numbers is", sum)
这就是它目前的工作方式:
How many numbers do you want to count together: 5
Enter the value of the number 1: 1
Enter the value of the number 2: 2
Enter the value of the number 3: 3
Enter the value of the number 4: 4
Enter the value of the number 5: 5
The value of the counted numbers is: 15
现在,棘手的部分来了:如果两个 CONSECTUTIVE 输入都为零 (0),则程序应该结束。如何创建一个变量来检查程序中每个 CONSECTUTIVE 输入的值是否不为零?我需要创建另一个循环还是什么?
这就是我希望程序工作的方式:
How many numbers do you want to count together: 5
Enter the value of the number 1: 1
Enter the value of the number 2: 0
Enter the value of the number 3: 0
Two consectutive zero's detected. The program ends.
提前致谢!
【问题讨论】:
标签: python loops input integer counting