我是新手編程,並試圖使用戶輸入接受整數和字符串。我已經嘗試了以下編碼..但不知道我錯了哪裏。有什麼建議麼?用戶輸入接受str和int(python)
noAdults = input("Please enter the number of adults:")
while noAdults.isalpha():
print("Number must be valid and >= 0")
if noAdults.isdigit() and noAdults < 0:
print("Error")
noAdults = input("Please enter the number of adults:")
ValueError異常:無效字面對於int()與基體10:
我猜測有一個ValueError因爲我已經使用了可變noAdults與因而isalpha並且因爲它是在一個int正在錯誤?
一個完全工作的版本,目前的代碼不會如果它有一個字母,再次檢查新的'noAdults'。 – Leigh