我正在制作一个数字猜谜游戏,涵盖4个.py文件和1个.txt文件。问题是在part2上(有:main.py,part1.py,part2.py和part3.py)我得到一个语法错误。(标题只是让我被允许发布问题。) 这是我的代码。
while (gussestaken < guesses):
try:
guess = int(input("Take a guess: "))
gussestaken = (guessestaken + 1)
if guess > maxnum or guess < 0:
print('Please, %s, enter a valid choice. Remember the maximum is %d.' % (name, maxnum))
在最后一个括号之后解析时,我得到一个语法错误意外的EOF。
答案 0 :(得分:0)
如果您尝试捕获ValueError
(当用户输入字符串时),请尝试以下操作:
while (gussestaken < guesses):
try:
guess = int(input("Take a guess: "))
gussestaken = (guessestaken + 1)
if guess > maxnum or guess < 0:
print('Please, %s, enter a valid choice. Remember the maximum is %d.' % (name, maxnum))
except ValueError:
print "Opps! You entered a string."
break