[英]Write a program that asks a user to enter two integer numbers and check if either one is another one’s multiple
[英]we need to write a prorgam that asks a user to enter multiple numbers and ends the while loop with -1.then work out the average of numbers provided
我们的任务是在 python 中编写一个程序,要求用户输入多个数字。 主题是while循环,所以我们只能使用while循环和if语句。 当用户想停止输入数字时,用户需要输入'-1'。 一旦完成,程序必须返回用户输入的数字的平均值。
这是我到目前为止所拥有的:#task 13-while.py
#first the program will explain to the user that the user can keep
#entering numbers until -1 occurs.
num = int(input('''please enter any number of your choice\n
please enter -1 to stop entry and run program'''))
num_count = 0
while num > -1:
num_count = num_count + 1
average = sum(num)/num_count
if num == -1:
print("the average of the numbers you have entered is"+ average)
对 python 非常缺乏经验,我们将不胜感激。
您需要将输入放入 while 中,然后仅在循环结束时计算平均值
像这样:
num = int(input("enter numbers and then -1 to stop: "))
num_count = 1
sum = num
while num!=-1:
num = int(input("another number: "))
num_count+=1
sum+=num
print(sum/num_count)
为了使其工作,您需要在while
循环中添加一个input()
调用,如下面的代码所示:
count = 0
sum = 0
num = int(input('''please enter any number of your choice\n
please enter -1 to stop entry and run program\n'''))
while num != -1:
sum += num
count +=1
num = int(input('''Give another integer\n'''))
print("the average of the numbers you have entered is", sum/count)
个人建议:我建议您阅读更多教程或要求您的peofessor进行更多工作
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.