# This is a guess the number game. See https://inventwithpython.com/chapter4.html import random guessesTaken = 0 print('Hello! What is your name?') myName = input() number = random.randint(1, 20) print('Well, ' + myName + ', I am thinking of a number between 1 and 20.') #while guessesTaken < 6: # commented out line, using instead a for loop for i in range(6): # loop for variable i starting at 0, counting up 1, 2, ..., to 6 print('Take a guess.') # There are four spaces in front of print. guess = input() # prompt guess guess = int(guess) # convert player's guess into an integer, hopefully this will be a number input guessesTaken = guessesTaken + 1 # keep track of the number of guesses taken by the player if guess < number: print('Your guess is too low.') # There are eight spaces in front of print. if guess > number: print('Your guess is too high.') if guess == number: break if guess == number: guessesTaken = str(guessesTaken) print('Good job, ' + myName + '! You guessed my number in ' + guessesTaken + ' guesses!') if guess != number: number = str(number) print('Nope. The number I was thinking of was ' + number)
Run
Reset
Share
Import
Link
Embed
Language▼
English
中文
Python Fiddle
Python Cloud IDE
Follow @python_fiddle
Browser Version Not Supported
Due to Python Fiddle's reliance on advanced JavaScript techniques, older browsers might have problems running it correctly. Please download the latest version of your favourite browser.
Chrome 10+
Firefox 4+
Safari 5+
IE 10+
Let me try anyway!
url:
Go
Python Snippet
Stackoverflow Question