0
我試圖做一些事情(承認)簡單。我想讓我的函數接受一個輸入,如果它是一個整數,運行幾行(如果它是一個特定命令(完成),則退出循環)或如果它是字符串,則輸出錯誤消息或。我的問題是,它從來沒有檢測到一個整數,而是總是顯示錯誤消息,除非我退出循環。檢查輸入值是整數,命令,還是隻是壞數據(Python)
#The starting values of count (total number of numbers)
#and total (total value of all the numbers)
count = 0
total = 0
while True:
number = input("Please give me a number: ")
#the first check, to see if the loop should be exited
if number == ("done"):
print("We are now exiting the loop")
break
#the idea is that if the value is an integer, they are to be counted, whereas
#anything else would display in the error message
try:
int(number)
count = count + 1
total = total + number
continue
except:
print("That is not a number!")
continue
#when exiting the code the program prints all the values it has accumulated thus far
avarage = total/count
print("Count: ", count)
print("Total: ", total)
print("Avarage: ", avarage)
從周圍的代碼有點戳,好像問題出在(數=計+ 1)和(總=總+ 1),但我無法明白。任何幫助不勝感激。