只是想知道我將如何存儲平方根平方和問題的輸入。如何存儲來自用戶的任意數量的輸入?
用戶可以輸入儘可能多的,因爲他們,直到他們鍵入「結束」數字
這裏是我爲
while(True):
x = input("Enter a number please:")
if x == 'end':
break
只是想知道我將如何存儲平方根平方和問題的輸入。如何存儲來自用戶的任意數量的輸入?
用戶可以輸入儘可能多的,因爲他們,直到他們鍵入「結束」數字
這裏是我爲
while(True):
x = input("Enter a number please:")
if x == 'end':
break
你想了解接下來的事情是數據結構如此list。然後您可以使用list.append
即時將項目添加到列表中。
不過,值得注意的是,你從哪裏學習Python,肯定會在某個或那個點上超越列表。
您可以將這些值附加到列表中。總和,然後除以平均長度。
nums = []
while True:
x = input("Enter a number:")
if x == 'end':
avg = sum(nums)/len(nums)
print("And the average is....", avg)
break
elif x.isdigit():
nums.append(int(x))
else:
print("Try again.")
如果您的意思是您要存儲所有給定的x值,請使用列表。 l = [] l.append(x)。
感謝我的一個好友建議我追加值以及不太確定如何做到現在! –