from random import randint firstNum = randint(0, 9) secondNum = randint(0, 9) while firstNum == secondNum: secondNum = randint(0, 9) numbers = [firstNum, secondNum] print("I'm thinking of two numbers between 0 and 9...can you guess what either one of them is?") print("You have 3 tries to get it right") print("") chances = 3 def check(x): try: guess = int(x) return guess except ValueError: print("Please enter a whole number") print("") play() def play(): userguess = input("Enter a guess: ") guess = check(userguess) if guess in numbers: win(guess) state = 1 return state else: tries = 2 - attempts if tries == 0: gameover() else: print("") print("{} was neither of my two numbers :( - Try again!".format(guess)) print("You have {} tries left".format(tries)) print("") state = 0 return state def gameover(): print("") print("You have run out of tries - Game Over! The numbers were {} and {}".format(numbers[0], numbers[1])) print("") def win(x): print("") print("You Win!") print("The two numbers were {} and {}. Your guess was {}".format(numbers[0], numbers[1], x)) print("") for attempts in range(chances): state = play() if state == 0: continue if state == 1: break
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