我可以弄清楚如何讓這段代碼接受浮點數以及整數。修改代碼以接受任何實數(整數或浮點數)
這段代碼的作用是:接受無限量的用戶輸入,必須作爲非負整數。當檢測到空行時,輸入結束。
該代碼;按升序打印列表。打印所有數字的總數,然後打印平均數字。
代碼:
nums = []
response = " "
total = 0
print("Type a positive integer: ")
while response != "":
response = input()
if response.isnumeric():
nums.append(int(response))
print("Next number: ")
elif response == '':
break
else:
print("Error, you have to type a non-negative integer.")
nums.sort()
for item in nums:
total = total + item
if nums != []:
print("The numbers in order: ",nums)
print("Total number: ",total)
print("The average is",sum(nums)/len(nums))
else:
print("There are no numbers in the list")
問題是什麼,如果你能弄明白的話? – usr2564301