Hey guys, im getting a syntax error on the "your guess is too high string" but i dont get it
Help is appreciated
P.S- this is a number guessing gameCode:import random print 'Hello, what is your name?' userName = raw_input() print ' Hi ' + userName + ', I am thinking of a number between 1 and 20, can you guess what it is?' random = random.randint(1,20) guess = int(raw_input('Take a guess:')) if guess > random: print 'Your anserw is too high' elif guess < random: print 'Your anserw is too low' elif guess == random: print 'Good job ' + userName + ', you guessed my number" print 'The game is over"
You're mixing your quotation marks.
The line
should beCode:print 'Good job ' + userName + ', you guessed my number"
There is a similar problem with the last line as well.Code:print 'Good job ' + userName + ', you guessed my number'
Are you planning on making the game loop until the right number is guessed?
You should use input() instead of int(raw_input()).
And if the person doesn't guess the number at first then he'll never find out the number you thought. So you should add an while loop.
The code should be something like this:
Code:from random import randint userName = raw_input("What's your name ?") print 'Hi' + userName + ', I am thinking of a number between 1 and 20, can you guess what it is?' random = randint(1,20) guess = input('Take a guess') while guess != random: if guess > random: print 'Your guess was too high.' else: print 'Your guess was too low.' guess = input('Take a guess') print 'Your guess was right.'
Better?Code:import random, time number = random.randint(1,10) guessesTaken = 0 print 'Hello, what is your name?' userName = raw_input() print 'Hi ' + userName + ', I am thinking of a number between 1 and 10, can you guess what it is?I will give you 3 tries' while guessesTaken < 3: guess = input('take a guess ') guessesTaken = guessesTaken + 1 if guess > number: print 'your guess was too high' if guess < number: print 'your guess was too low' if number == guess: break if guess == number: guessesTaken = str(guessesTaken) print 'Good job ' + userName + ', you got it in ' + guessesTaken + ' tries' if guess != number: number = str(number) print 'Sorry ' + userName + ', but the number was ' + number
How would I go about making an option to play again?
You can simply add another while loop.
Here's an example code:
Why did you add the time module ?Code:import random print 'Hello, what is your name?' userName = raw_input() loop = 0 while loop == 0: number = random.randint(1,10) print 'Hi ' + userName + ', I am thinking of a number between 1 and 10, can you guess what it is?I will give you 3 tries' guessesTaken = 0 while guessesTaken < 3: guess = input('take a guess ') guessesTaken = guessesTaken + 1 if guess > number: print 'your guess was too high' elif guess < number: print 'your guess was too low' elif number == guess: break if guess == number: guessesTaken = str(guessesTaken) print 'Good job ' + userName + ', you got it in ' + guessesTaken + ' tries' elif guess != number: number = str(number) print 'Sorry ' + userName + ', but the number was ' + number start = raw_input('Do you wish to play again? (y/n)') if start != 'y': loop = 1
Posted via CodeCall Mobile I was just messing with some time functions and forgot to take it out, but thanks for the help
There are currently 1 users browsing this thread. (0 members and 1 guests)
Bookmarks