Skip to content

added condition to check number range and updated the print to f-strings #1

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
52 changes: 32 additions & 20 deletions guess_the_number.py
Original file line number Diff line number Diff line change
@@ -1,20 +1,32 @@
import random
number = random.randint(1, 10)

player_name = input("Hello, what is your name? ")
number_of_guesses = 0
print('I\'m glad to meet you! {} \nLet\'s play a game with you, I will think a number between 1 and 10 then you will guess, alright? \nDon\'t forget! You have only 3 chances so guess:'.format(player_name))

while number_of_guesses < 3:
guess = int(input())
number_of_guesses += 1
if guess < number:
print('Your estimate is too low, go up a little!')
if guess > number:
print('Your estimate is too high, go down a bit!')
if guess == number:
break
if guess == number:
print( 'Congratulations {}, you guessed the number in {} tries!'.format(player_name, number_of_guesses))
else:
print('Close but no cigar, you couldn\'t guess the number. \nWell, the number was {}.'.format(number))
import random
number = random.randint(1, 10)

player_name = input("Hello, what is your name? ")

number_of_guesses = 0

print('I\'m glad to meet you! {} \nLet\'s play a game with you, I will think a number between 1 and 10 then you will guess, alright? \nDon\'t forget! You have only 5 chances so guess:'.format(player_name))

while number_of_guesses < 5:
guess = int(input())

number_of_guesses += 1

if guess > 10 or guess < 1: #checking if within range
print(f'Number of out of range. The number must be between 1 and 10')
print(f'You now have {5 - number_of_guesses} chances left')
elif guess < number: #checking if less than the correct number
print(f'Your estimate is too low, go up a little!')
print(f'You now have {5 - number_of_guesses} chances left')
elif guess > number: #checking if higher than the correct number
print(f'Your estimate is too high, go down a bit!')
print(f'You now have {5 - number_of_guesses} chances left')
else: # The number is correct
print(f'Congratulations {player_name}, you guessed the number in {number_of_guesses} tries!')
break
if number_of_guesses == 5:
break
else:
continue

print(f'Close but no cigar, you couldn\'t guess the number. \nWell, the number was {number}')