0
問題出在標題中:你如何轉到else部分的if語句的開頭?你如何去到else部分的if語句的開始處? Python 3.2
代碼:循環中的
p1 = int(input())
if p1 <= 9 and p1 >= 1:
pass
else:
print('ERROR 404. Invalid input. Please try again.')
p1 = input()
問題出在標題中:你如何轉到else部分的if語句的開頭?你如何去到else部分的if語句的開始處? Python 3.2
代碼:循環中的
p1 = int(input())
if p1 <= 9 and p1 >= 1:
pass
else:
print('ERROR 404. Invalid input. Please try again.')
p1 = input()
運行,從不打出來,直到輸入符合標準。
while True:
p1 = int(input("input something: "))
if p1 <= 9 and p1 >= 1:
break
print('ERROR 404. Invalid input. Please try again.')
如果輸入無法轉換爲int
和終止程序的值。此代碼會拋出異常。
爲了避免這種情況發生,並繼續進行。
while True:
try:
p1 = int(input("input something: "))
if p1 <= 9 and p1 >= 1:
break
except ValueError:
pass
print('ERROR 404. Invalid input. Please try again.')
聽起來像你需要'循環'? – corn3lius
我通常在這種情況下使用循環。如果輸入有效繼續。其他循環。我不認爲你可以像Python中的goto一樣在python中跳轉語句 –