Boibot 发表于 2022-5-11 23:39:39

SyntaxError: 'break' outside the loop

# This is a guess the number game.
import random

print('Hello. What is your name?')
name = input()

print('Well, ' + name + ',I am thinking a number between 1 and 20.')
secretNumber = random.randint(1,20)

# print('Debug: Secret number is ' + str(secretNumber))

for guessesTaken in range(1,7):
    print('Take a guess.')
    guess = int(input())
    try:
      if guess < secretNumber:
            print('Your guess is too low.')
      elif guess > secretNumber:
            print('Your guess is too high.')
      else:
            break # This condition is for the correct guess!
    except ValueError:
      print('You did not enter a number.')
   
if guess == secretNumber:
    print('Good job, ' + name + '! You guessed my number in ' +str(guessesTaken) +' guesses!')
else:
    print('Nope. The number I was thinking of was ' + str(secretNumber))

Twilight6 发表于 2022-5-11 23:45:13



SyntaxError: 'break' outside the loop

这个报错主要是指你在非循环体中使用了 break 关键字

只要将 break 关键字都使用在循环内即可

你代码中并没有发现会抛出这个错误的问题

isdkz 发表于 2022-5-11 23:45:47

本帖最后由 isdkz 于 2022-5-11 23:47 编辑

你这个没有问题,标题那个错误是在 break 没有放在循环里面才会出现的

Boibot 发表于 2022-5-11 23:52:32

我用pythontutor检验这段代码,指出SyntaxError: 'break' outside the loop
我用F5跑一下,抛出来line 14, in <module>
    guess = int(input())
ValueError: invalid literal for int() with base 10: 'ss'
这样except ValueError等于没有设置

Boibot 发表于 2022-5-12 00:22:31

Twilight6 发表于 2022-5-11 23:45
这个报错主要是指你在非循环体中使用了 break 关键字

只要将 break 关键字都使用在循环内即可


请往下翻一下

Boibot 发表于 2022-5-12 00:23:02

isdkz 发表于 2022-5-11 23:45
你这个没有问题,标题那个错误是在 break 没有放在循环里面才会出现的

请往下翻一下

Twilight6 发表于 2022-5-12 07:16:34

Boibot 发表于 2022-5-11 23:52
我用pythontutor检验这段代码,指出SyntaxError: 'break' outside the loop
我用F5跑一下,抛出来
这样ex ...


这个报错是因为你 guess 那输入的是字符串 'ss' ,而 'ss' 在 try 捕获外,导致没有捕获到这个ValueError

而且建议在循环之前对数值进行初始化,这样可以更安全的执行代码,参考代码:

# This is a guess the number game.
import random

print('Hello. What is your name?')
name = input()

print('Well, ' + name + ',I am thinking a number between 1 and 20.')
secretNumber = random.randint(1, 20)

# print('Debug: Secret number is ' + str(secretNumber))
guess = 0
guessesTaken = 0
for guessesTaken in range(1, 7):
    print('Take a guess.')
    try:
      guess = int(input())
      if guess < secretNumber:
            print('Your guess is too low.')
      elif guess > secretNumber:
            print('Your guess is too high.')
      else:
            break# This condition is for the correct guess!
    except ValueError:
      print('You did not enter a number.')

if guess == secretNumber:
    print('Good job, ' + name + '! You guessed my number in ' + str(guessesTaken) + ' guesses!')
else:
    print('Nope. The number I was thinking of was ' + str(secretNumber))

Boibot 发表于 2022-5-12 12:15:15

Twilight6 发表于 2022-5-12 07:16
这个报错是因为你 guess 那输入的是字符串 'ss' ,而 'ss' 在 try 捕获外,导致没有捕获到这个ValueE ...

怎么改写能让try捕获'ss'异常

Boibot 发表于 2022-5-12 14:51:17

Twilight6 发表于 2022-5-12 07:16
这个报错是因为你 guess 那输入的是字符串 'ss' ,而 'ss' 在 try 捕获外,导致没有捕获到这个ValueE ...

怎么改写能让try捕获'ss'异常

Twilight6 发表于 2022-5-12 15:05:19

Boibot 发表于 2022-5-12 14:51
怎么改写能让try捕获'ss'异常

6 楼的代码就可以
页: [1]
查看完整版本: SyntaxError: 'break' outside the loop