[英]Using while loop to count the number of user inputs
我尝试创建一个 while 循环函数,其中程序采用浮点数(米)形式的六个单独的用户输入。 如果数字大于零,则接受每个输入。 如果数字小于零,则取消每个输入。
While 循环应该计算最多六个用户输入,然后打印:
我的挑战是我不明白如何使用 while 循环,以便它计数到一定数量的输入,然后停止
以下是示例输出:
Give the latest user input: 86.2
Give the latest user input: 81.2
Give the latest user input: 79.6
Give the latest user input: 89.3
Give the latest user input: -1
Give the latest user input: 86.5
There were 5 succesful input(s).
There were 1 dismissed input(s).
The highest input was 89.3.
The mean of all inputs was 84.56 meters.
怎么样:
nums = []
while len(nums)<6:
n = int(input())
if n > 0:
nums.append(n)
print(nums)
print(min(nums))
print(max(nums))
给予:
[2, 2, 3, 4, 1, 4]
1
4
尝试
sum=0
count=0
good_count=0
max_val=-1
while count<6:
number = int(input())
if number > 0:
sum=sum + number
good_count=good_count+1
if number>max_val:
max_val=number
count=count + 1
print('there were {0} valid inputs'.format(good_count))
print('there were {0} invalid inputs'.format(count-good_count))
if good_count>0:
print('the average value was ' ,sum/good_count)
print('the maximum input values was ', max_val)
else:
print('there were no valid inputs so an average cannot be calculated')
print('there were no valid inputs so a maximum value does not exist')```
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.