Categories
Code Python Tutorial

Learning Python (Automate the boring Stuff) Chapter 3 problem’s alternative solution

Number Guessing Game in python!

I was learning python from this wonderful video tutorial and I usually try the problem before the tutor gives the solution. I found out that my solution is better and more full proof than theirs. It’s probably because they are still teaching the concepts and not worried about the details. I thought I could share what I wrote. Let me know if you have better solution and if you find any bug in mine.

You can also learn this from the website

http://automatetheboringstuff.com/chapter3/

#This is guess the number game
import random

print('Hello, What is  your name?')
name = input()
print('well ' + str(name) + ', I am thinking of a number between 1 and 20.')

myGuess = random.randint(1, 20) #between and including 1 & 20


for guessesTaken in range(1, 7):
    print('Take a guess')
    guess = input()
    try:
        if guessesTaken < 6:
            if int(guess)> myGuess:
                print('Your number is too High')
            elif int(guess)< myGuess:
                print('Your number is too low')
            elif int(guess)== myGuess:
                print('You got that right in '+ str(guessesTaken) + ' attemp(s)')
                break
        else:
             print('okay you tried ' + str(guessesTaken) + ' times you failed, it was ' + str(myGuess))
            
    except ValueError:
        print(' You didn\'t enter a number')