Skip to content

Instantly share code, notes, and snippets.

@ArianK16a
Last active February 26, 2020 10:44
Show Gist options
  • Save ArianK16a/4543639b97507a9516d415714d2b0854 to your computer and use it in GitHub Desktop.
Save ArianK16a/4543639b97507a9516d415714d2b0854 to your computer and use it in GitHub Desktop.
Python guess my number game
import random
print('Hi, Lets play a guess my number game. What is your name?')
name = input()
print(f'Hello {name}, Lets begin!')
def invalid_number():
print('Error: Your choice is invalid! Try again and choose a natural number.')
return(0)
def set_max_num():
global correctNumber, max_number
try:
print('What should the max number be?')
max_number = int(input())
correctNumber = random.randint(1, max_number)
except ValueError:
invalid_number()
set_max_num()
set_max_num()
def set_guesses():
global guesses
try:
print('How many guesses do you want to have?')
guesses = int(input())
except ValueError:
invalid_number()
set_guesses()
if guesses < 1:
invalid_number()
set_guesses()
set_guesses()
def take_guess():
global guess, guesses
print (f'Take a guess between 1 and {max_number}. This is try {tries} out of {guesses}.')
try:
guess = int(input())
except ValueError:
invalid_number()
take_guess()
if guess not in range(1, max_number + 1):
print(f'Your guess is not in the range from 1 to {max_number}!')
take_guess()
for tries in range(1, guesses + 1):
take_guess()
if guess > correctNumber:
print ('Your guess is too high!')
elif guess < correctNumber:
print('Your guess is too low!')
else:
break
def calculate_score():
print(f'You are Pro af, {name}.')
if guess == correctNumber:
calculate_score()
print()
print(f'Congratulations {name}, you guessed number {correctNumber} correctly.')
else:
print()
print(f'You failed. I was thinking about number {correctNumber}.')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment