[英]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.