我需要在python中創建一個更改計算器,並且我想讓用戶無法輸入字母或符號,我嘗試了很多東西,讓它工作。我使用了一個while循環來阻止用戶輸入0.01以下的任何內容,因此它必須可以對字母進行操作。如何讓用戶不能輸入任何非數字字符
given = float(input("How much money was given?\n"))#Asks for the amount of money given
while given < 0.01:#Prevents any digits 0 or lower from being inputted
print("That is not a valid amount")
given = float(input("How much money was given?\n"))
if given > 0.01:
break
while True:#Prevents anything not a digit from being inputted
print("That is not a valid option")
while given.isalpha():#Prevents anything not a digit from being inputted
print("That is not a number")
given = float(input("How much money was given?\n"))
錯誤消息說,它不能改變串到整數/浮動
一些其他位不工作,因爲我是從網上嘗試不同的事情,但我特別需要與i位被問及幫助。再次感謝
[詢問用戶輸入的,直到他們得到一個有效的響應]的可能的複製(http://stackoverflow.com/questions/23294658/asking-the-用戶輸入,直到他們給一個有效的迴應) – jonrsharpe
你可以發佈你已經嘗試過,爲什麼它不工作? – Aaron