其實實現這個功能很是簡單, 只須要使用一個while循環就行了, 若是知足條件就break跳出循環. 示例代碼以下:python
#input and judge gender
while True:
gender = input("Please input your gender (m for male, f for female):")
if(gender =='m' or gender == 'f'):
break
else:
print("Error! you must input a valid letter!Please try again!\n")
continue
複製代碼