When running 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 break the while-loop not the if-else loop which I think might be what's 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"
You are using break statement , just after the while loop, outside the while loop, if you want to exit from the script, you should use
sys.exit()
. The issue is in the lines -At the start of the script, you should import
sys
as -