Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #number guessing game
- #welcome the user
- #users should input ranges, upper range and lower range
- #import random
- #create a while loop
- #import math
- import random
- print "Number guessing game"
- name_of_gamer = input("Enter your name:\n")
- print "welcome: " + name_of_gamer
- approval = input("Do you want to play this game?\n")
- if approval.lower() == "yes":
- print "Let's go!"
- else:
- print "It was nice having you here " + name_of_gamer
- quit()
- # Enter range limits
- upper_range = int(input("Enter any number for your upper range:\n"))
- lower_range = int(input("Enter any number for your lower range:\n"))
- # Swap range limits if required
- if upper_range < lower_range:
- upper_range, lower_range = lower_range, upper_range
- # Show the actual range that was entered
- print "Upper limit = " + str(upper_range)
- print "Lower limit = " + str(lower_range)
- # Choose a random number within the range
- secret_number = random.randint(lower_range, upper_range)
- # Show the secret number (for testing)
- # print "The secret number = " + str(secret_number)
- # Get gamer's first guess
- gamers_guess = int(input("Enter a guess:\n"))
- # Guessing loop
- while gamers_guess != secret_number:
- # Too high?
- if gamers_guess > secret_number:
- # Get another guess
- print("print your guess is above the number")
- gamers_guess = int(input("Enter another guess:\n"))
- else: # Too low
- # Get another guess
- print("print your guess is below the number")
- gamers_guess = int(input("Enter another guess:\n"))
- # Cool!
- print "We have a winner!"
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement