mercredi 17 juin 2015

SyntaxError: 'break' outside loop in python code

when runnin this code i keep getting the error "SyntaxError: 'break' outside loop in python code" after i enter any number (the first raw-input) i am trying to brake the while loop not the if else loop which i think might be whats happening?

     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"

Aucun commentaire:

Enregistrer un commentaire