Pages


Tuesday, 4 December 2018

(Post 7/Year 1 Week 1)Learning python part 7:GUI(Graphic User Interface) + number guessing game

In this post,we will be using the knowledge from the previous post to create a number guessing game with GUI

It is Similar to post 3 number guessing game but with GUI

Code

##import the module random and easygui
import random, easygui


##set secret as a random number from 1 to 99 

secret = random.randint(1, 99)

##set guess to 0 
guess = 0

##set tries to 0 
tries = 0


##print out the message in the easygui msgbox 
easygui.msgbox("""AHOY! I'm the Dread Pirate Roberts, and I have a secret!
It is a number from 1 to 99. I'll give you 6 tries.""")



##while guest is not equal to secret and your tries is less than 6,gui will ask for your guess 
while guess != secret and tries < 6:
##integerbox to ask what is your guess 
 guess = easygui.integerbox("What's yer guess, matey?")

##if guess is nothing,break will indicate exit out of loop 
 if not guess:
  break
##if guess is less than secret 
 if guess < secret:

##gui will print out your input and print out is too low 
    easygui.msgbox(str(guess) + " is too low, ye scurvy dog!")



##elif guess is more than secret 

 elif guess > secret:

##gui will print out your input and print that your guess is too high 
     easygui.msgbox(str(guess) + " is too high, landlubber!")

##tries will increase by 1 
 tries = tries + 1



##if guess is equal to secret 
if guess == secret:



##gui will print that you have found his secret message 

 easygui.msgbox("Avast! Ye got it! Found my secret, ye did!")


##else 

else:

##if your tries exceeds 6 times,gui will print out the message and try you no more try 
 easygui.msgbox("No more guesses! The number was " + str(secret)) 
Output


Click ok,try 80



result,click ok


click ok,try again,60


result,click ok


click ok,try again 70


result,click ok


click ok,try again 75


result,click ok



click ok,try again 74



result,click ok



No comments:

Post a Comment