SyntaxError:python代码中的'break'外部循环

时间:2015-06-17 14:58:11

标签: python if-statement raw-input

运行此代码时,在输入任意数字(第一个原始输入)后,我不断收到错误SyntaxError: 'break' outside loop in python code。我试图打破while循环而不是if-else循环,我认为可能是正在发生的事情。

     import time
     import random

     players = raw_input("1 or 2 players? please enter ")

if (players==1):

    name = raw_input("What is your name? ")

    print "Hello, " + name, "Time to play hangman!"

    print "\n"

    time.sleep(1)   #wait for 1 second

    print "Start guessing... you only get 10 lives!"
    time.sleep(0.5)

    #here we set the secret
    word = 'ant car space stas satalite hubble amaing baboon badger bat bear      beaver camel cat clam cobra cougar coyote crow deer dog donkey duck eagle ferret     fox frog goat goose hawk lion lizard llama mole monkey moose mouse mule newt     otter owl panda parrot pigeon python rabbit ram rat raven rhino salmon seal     shark sheep skunk sloth snake spider stork swan tiger toad trout turkey turtle     weasel whale wolf wombat zebra'.split()
    def getRandomWord(wordList):
        # This function returns a random string from the passed list of strings.
        wordIndex = random.randint(0, len(wordList) - 1)
        return wordList[wordIndex]

    guesses = ''     #creates an variable with an empty value

    turns = 10     #determine the number of turns

    # Create a while loop

    while turns > 0:            #check if the turns are more than zero

        failed = 0        # make a counter that starts with zero           

        for char in word:          # for every character in secret_word  

            if char in guesses:            # see if the character is in the players guess

                print char,            # print then out the character

        else:

            print "_",             # if not found, print a dash


            failed += 1     # and increase the failed counter with one

    if failed == 0:         # if failed is equal to zero
    print "\nYou won"  

    break          # exit the script      

    print

     guess = raw_input("guess a character:")    # ask the user go guess a     character

    guesses += guess          # set the players guess to guesses           

    if guess not in word:   # if the guess is not found in the secret word

        turns -= 1        # turns counter decreases with 1 (now 9)

        print "Wrong\n"    

        print "You have", + turns, 'more guesses'    # how many turns are left

        if turns == 0:            # if the turns are equal to zero

            print "You Loose\n"       # print "You Loose"

  else:
     player1= raw_input("whats the secret word?")
     #welcoming the user
     name2= raw_input("What is the name of the guesser?")

    print "Hello, " + name2, "Time to play hangman!"

    print "\n"

    time.sleep(1)     #wait for 1 second

    print "Start guessing... you only get 10 lives!"
    time.sleep(0.5)
    word2 = player1
    guesses = ''     #creates an variable with an empty value

    turns = 10     #determine the number of turns

    # Create a while loop

    while turns > 0:            #check if the turns are more than zero

        failed = 0        # make a counter that starts with zero           

        for char in word:          # for every character in secret_word  

            if char in guesses:            # see if the character is in the   players guess

                print char,            # print then out the character

        else:

            print "_",             # if not found, print a dash

            failed += 1     # and increase the failed counter with one

     if failed == 0:         # if failed is equal to zero
         print "\nYou won"  

    break         # exit the script      

    print

    guess = raw_input("guess a character:")    # ask the user go guess a character

     guesses += guess          # set the players guess to guesses           

    if guess not in word:   # if the guess is not found in the secret word

        turns -= 1        # turns counter decreases with 1 (now 9)

        print "Wrong\n"    

        print "You have", + turns, 'more guesses'    # how many turns are left

        if turns == 0:            # if the turns are equal to zero

             print "You Loose\n"       # print "You Loose"

2 个答案:

答案 0 :(得分:2)

您正在使用break语句,就在while循环之后,在while循环之外,如果要退出脚本,则应使用sys.exit()。问题在于 -

while turns > 0:            #check if the turns are more than zero

    failed = 0        # make a counter that starts with zero           

    for char in word:          # for every character in secret_word  

        if char in guesses:            # see if the character is in the players guess

            print char,            # print then out the character

    else:

        print "_",             # if not found, print a dash


        failed += 1     # and increase the failed counter with one

if failed == 0:         # if failed is equal to zero
    print "\nYou won"  

sys.exit()          # exit the script      

在脚本开头,您应该导入sys作为 -

import sys

答案 1 :(得分:0)

if语句是否在while转动> 0循环内?这里的问题是缩进不匹配。 if语句位于while循环之外,break语句位于同一级别。