Stap 14: Nummer gissen programma
import random number =int(raw_input("Guess a number (between 1 and 100):")) # initially ask the user for a number randNumber = random.randint(1,100) # create a random numberwhile number != randNumber: # if the user guessed correctly on first try this whole loop is skipped number =int(raw_input("You didn't guess right, guess again:")) # re-store a user-input number, it will get checked automatically at the end of the loopprint"You guessed correctly!"# the only way you can reach this line of code is if the user guessed correctly.
OK, is dit erg moeilijk. Dus waarom niet maken het weer als je te hoog of te laag geraden?
Nogmaals, uw oplossing kan verschillen:
import random number =int(raw_input("Guess a number (between 1 and 100):")) randNumber = random.randint(1,100) while number != randNumber: if number > randNumber: print"You guessed to high"else: print"You guessed to low" number =int(raw_input("Guess again:")) print"You guessed correctly!"
*.Step-body > pre, de pre {background-color: rgb(39,40,34); kleur: rgb(248,248,242); padding: 5.0px;}