try:
num = int(input("Give me an integer between 1 and 100:"))
while num > 100 or num < 1:
print ("Sorry, that is not an integer between 1 and 100. Try again.")
num = int(input("Give me an integer between 1 and 100:"))
except:
print ("Sorry, that is not an integer between 1 and 100. Try again.")
else:
print ("Thank you for your input")
如何告訴Python如果輸入了諸如「pear」或「sd23214」之類的輸入,還打印出「sorry try again」?謝謝。python試試格式除外
1.你爲什麼認爲這不是這樣做的? 2.你有什麼嘗試? 3.爲什麼你有沒有「如果」的「其他」? – Colleen
只需在'while'循環中放入所有相關內容即可。 – irrelephant
@Colleen,否則可以使用'except'和'for'循環以及'if' –