[英]I cant get this code to work in python
keepgoing = True
num1 = int(input("Enter a Number"))
num2 = int(input("Enter a Number"))
bignums = 0
smallnums = 0
counter = 0
while keepgoing:
if num1 > num2:
bignums = bignums + num1
smallnums = smallnums + num2
else:
bignums = bignums + num2
smallnums = smallnums + num1
counter + 1
if num1 == 0:
keepgoing = False
print (bignums / counter)
print (smallnums / counter)
The program i am writing: Write a program that enters pairs of numbers until the first number in the pair is 0. The program will add the smallest number to a total for smaller numbers, the largest number to a total for the largest numbers. 我正在编写的程序:编写一个输入数字对,直到该对中的第一个数字为0的程序。该程序将对较小的数字将最小的数字加到总数中,对于最大的数字将最大的数字加到总数中。 After exit from the loop, it will print the average of the smaller numbers, the average of the higher numbers, and the highest and lowest numbers entered.
从循环退出后,它将打印较小数字的平均值,较高数字的平均值以及输入的最高和最低数字。
I think i did everything right, but it wont end/exit the loop when you type "0". 我认为我所做的一切都正确,但是当您键入“ 0”时,它不会结束/退出循环。 Also i don't know how to make it show the highest and lowest number.
我也不知道如何显示最高和最低数字。 Can anyone help me please?
谁能帮我吗?
Do something like this as suggested by @Joseph keep the entry for numbers inside the loop: 按照@Joseph的建议执行以下操作,将数字的条目保留在循环内:
keepgoing = True
bignums = 0
smallnums = 0
counter = 0
while keepgoing:
num1 = int(input("Enter a First Number"))
num2 = int(input("Enter a Second Number"))
if num1 > num2:
bignums = bignums + num1
smallnums = smallnums + num2
else:
bignums = bignums + num2
smallnums = smallnums + num1
counter += 1
if num1 == 0:
keepgoing = False
print "Average of big numbers", bignums / counter
print "Average of small numbers", smallnums / counter
Also would be good if you handle cases like what will happen if the user enters both the numbers as same. 如果您处理类似用户输入两个数字时会发生的情况,那也很好。 Let me know if you need more help
让我知道您是否需要更多帮助
One thing to note is Num1 never changes so if you put something other then zero it would get stuck. 要注意的一件事是Num1永远不会改变,因此,如果您放置零以外的其他值,它将被卡住。 you can change
keepgoing = false
您可以更改
keepgoing = false
to 至
break
hope this helps! 希望这可以帮助!
num1 = int(input("Enter a Number"))
num2 = int(input("Enter a Number"))
must be inside the loop as well for the application to terminate. 还必须在循环内,以使应用程序终止。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.