Pages


Wednesday, 4 April 2018

(Post 2/Year 1 Week 1)Learning python part 2:Number guessing game

Number Guessing Game

##   ##comment
##  importing the module random,jupyter notebook has pre loaded with module

import random

##set the variable secret as a integer 1 to 99
secret = random.randint(1, 99)
print(secret)
##set the variable guess to zero
guess = 0

##set the variable guess to zero
tries = 0

##write the introduction of the number game
print("AHOY! I'm the Dread Pirate Roberts, and I have a secret!")
print ("It is a number from 1 to 99. I'll give you 6 tries. ")

##while guess is not equal to secret and the tries is less than 6
while guess != secret and tries < 6:
    guess = input("What's yer guess?")
    if int(guess) < secret:             ##if guess is less than secret
        print("Too low, ye scurvy dog!")
    elif int(guess) > secret:        ##else if guess is more than secret
        print("Too high, landlubber!")
    elif int(guess) == secret:   ##else if  guess is equal to secret
        print("Avast! Ye got it! Found my secret, ye did!")
        break

    tries = tries + 1      ##tries is equal to tries + 1,so it will increase by 1 per while loop

 
##if guess is equal to secret
if tries == 6:
    print("No more guesses! Better luck next time!")




Output

1st try:


After cheating



Do look out for part 3!

No comments:

Post a Comment