repl.it
@simontiger/

NumberGuessingGame

Python

The computer thinks of a number 1-100. Try to guess it within 7 takes!

fork
loading
Files
  • main.py
main.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
import math, random

def start_game(maximum):
  number = math.floor(random.uniform(0, maximum)) + 1
  guessed = input("Guess a number 1-" + str(maximum) + ": ")
  while True:
    try:
      if int(guessed) < number:
        guessed = input("Too low. Try again: ")
      elif int(guessed) == number:
        # Even though I say it's y, you play again is you say something that contains y or Y
        if input("You got it! Play again? (y/N): ").lower().find("y") != -1:
          print("")
          start_game(maximum)
        return
      else:
        guessed = input("Too high. Try again: ")
    except ValueError:
      guessed = input("Please enter a number. ")

start_game(100)