我是用 Python 3 写的,但是如何防止用户输入字符串?
x = int(input("If you want to play with computer, click 0. If you want to play with your friend, click 1. "))
使用try
/except
while True:
user_input = input("If you want to play with computer, click 0. If you want to play with your friend, click 1. ")
try:
user_input = int(user_input)
# do something
break
except ValueError:
print("input a valid choice please")
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句