如何在游戏中解决此语法错误?

时间:2019-08-21 04:05:56

标签: python

我正在创建一个用于练习的猜谜游戏,此语法错误出现在我的代码中,该错误表明我的break函数不在循环内。我查看了我的代码,它不在循环之外,所以如果有人可以帮助我解决我的问题,我将不胜感激。

from random import randint

def guessing_game():

    print('Welcome to the Guessing Game')
    print('If you would like to exit, just type exit')

    while True:

        num = randint(1,9)
        guesses = []

        while True:

            try:
                guess = int(input('Please guess a number between 1 and 9: '))
            except:
                print('Please enter a numerical value')
                continue
            else:
                if guess < 1 or guess > 9:
                    print('OUT OF RANGE')
                    continue
                else:
                    if guess == num:
                        print(f'Congratulations, you guessed the correct number in {len(guesses)} guesses')
                        break
                    else:
                        if guess > num:
                            print('TOO HIGH')
                            guesses.append(guess)
                            continue
                        else:
                            print('TOO LOW')
                            guesses.append(guess)
                            continue


    play_again = input('Enter exit to stop playing: ')

    if play_again == 'exit':
        break
    else:
        continue

这是我得到的错误:

  File "<ipython-input-1-09b3cd044357>", line 42
    break
    ^
SyntaxError: 'break' outside loop

2 个答案:

答案 0 :(得分:2)

检查最后几行的缩进,并确保它们在 while循环,它将起作用。

from random import randint

def guessing_game():
  print('Welcome to the Guessing Game')
  print('If you would like to exit, just type exit')

  while True:

      num = randint(1,9)
      guesses = []

      while True:

          try:
              guess = int(input('Please guess a number between 1 and 9: '))
          except:
              print('Please enter a numerical value')
              continue
          else:
              if guess < 1 or guess > 9:
                  print('OUT OF RANGE')
                  continue
              else:
                  if guess == num:
                      print(f'Congratulations, you guessed the correct number in {len(guesses)} guesses')
                      break
                  else:
                      if guess > num:
                          print('TOO HIGH')
                          guesses.append(guess)
                          continue
                      else:
                          print('TOO LOW')
                          guesses.append(guess)
                          continue


      play_again = input('Enter exit to stop playing: ')

      if play_again == 'exit':
        break
      else:
        continue

回复:https://repl.it/repls/TurbulentImpeccableObjectdatabase

答案 1 :(得分:0)

您的第42行不在第二 while loop 中,因此这就是为什么您会出错的原因,请确保它会在while循环中出现在您的行中:

if play_again == 'exit':
    break
else:
    continue

您可以复制以下代码:

from random import randint

def guessing_game():
  print('Welcome to the Guessing Game')
  print('If you would like to exit, just type exit')

  while True:

      num = randint(1,9)
      guesses = []

      while True:

          try:
              guess = int(input('Please guess a number between 1 and 9: '))
          except:
              print('Please enter a numerical value')
              continue
          else:
              if guess < 1 or guess > 9:
                  print('OUT OF RANGE')
                  continue
              else:
                  if guess == num:
                      print(f'Congratulations, you guessed the correct number in {len(guesses)} guesses')
                      break
                  else:
                      if guess > num:
                          print('TOO HIGH')
                          guesses.append(guess)
                          continue
                      else:
                          print('TOO LOW')
                          guesses.append(guess)
                          continue


      play_again = input('Enter exit to stop playing: ')

      if play_again == 'exit':
        break
      else:
        continue