为什么会出现语法错误

时间:2017-05-03 17:30:17

标签: python syntax

我在(turns-=3)上和else:上一直收到语法错误。我以为我正确设置了简单的代码。

from random import randint

rn=random.randomint(1, 10)

turns=3


while turns>0:
    guess=int(input('Guess a number  '))

    if guess==rn:
        print('YOU WIN!!')
        break

    elif guess!=rn:
        print('Nope, Try Again.'
    turns-=1          
else:
    print('Sorry you are out of turns. You lose, but better luck next time.')

1 个答案:

答案 0 :(得分:0)

具体错误是因为您未在上面的行中使用print关闭)语句。还有一些其他问题。我清理了你的代码,这对我而言:

from random import randint

rn=randint(1, 10)

turns=3

while turns>0:
  guess=int(input('Guess a number '))

  if guess==rn:
      print('YOU WIN!!')
      break

  elif guess!=rn:
      print('Nope, Try Again.')
      turns -= 1          
  else: print('Sorry you are out of turns. You lose, but better luck next time.')