Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/env python3
- #Guess the number week 7
- #This program is a number guessing game
- #Amanda Rhodes
- #12/14/19
- import random
- #Display Welcome Message and ask for a number
- table = [] #HERE
- d = 0 #HERE TOO
- count = 1
- my_number = random.randint(1, 10)
- #Random number, loop while true
- #User Guesses a number
- def option1():
- count = 1
- you_guessed=[]
- my_number = random.randint(1, 10)
- while True:
- print()
- try:
- user_guess = int(input("Guess a number between 1 and 10: "))
- print()
- except ValueError:
- print()
- print("Numbers only please!")
- continue
- if user_guess < 1 or user_guess > 10:
- print("Sorry, entry must be a number between 1 and 10.")
- continue
- if user_guess in (0, 11):
- break
- #tell user if guess is correct or not
- #If correct tell user how many attempts
- #List all numbers guessed
- if user_guess < my_number:
- print("Too Low!")
- count=count+1
- you_guessed.append(user_guess)
- elif user_guess > my_number:
- print("Too High!")
- count=count+1
- you_guessed.append(user_guess)
- elif user_guess == my_number:
- print("You Guessed it! It took you " + str(count) +" attempts.")
- print()
- you_guessed.append(user_guess)
- print("You picked the following numbers:" + str(you_guessed))
- #break out of loop
- break
- #my changes
- #DEFINED RANDOM FUNCTION,SO WE CAN CALL IT ANYTIME
- def up():
- randnum = random.randint(1,10)
- return randnum
- #My changes
- #DEFINED A LOOK-UP FUNCTION TO VERIFY IF ALREADY GUESSED
- #NUMBER EXISTS
- def lookup():
- global d
- net = up()
- if net in table:
- lookup()
- elif net not in table:
- table.append(net)
- d = len(table)
- net = net
- return net
- #computer guesses a number
- #Display guessed numbers and number of attempts
- def option2():
- global table #HERE ALSO
- print()
- computer_guess = int(input("Please enter a number between 1 and 10 for the computer to guess: "))
- print()
- count=1
- computer_guessed=[]
- while True:
- print()
- print()
- lookup() #HERE ALSO
- randomval = 0 #HERE TOO
- computer_guessed.append(randomval)
- for sip in range(0,len(table)):#HERE ALSO
- randomval =table[sip]
- if randomval< computer_guess:
- print("The computer guessed ", [randomval] ," which is too low")
- continue
- elif randomval> computer_guess:
- print("The computer guessed ", [randomval]," which is too high")
- #count=count+1
- continue
- else:
- computer_guess ==randomval
- print("The computer guessed ", randomval," which is correct. It took " + str(len(table)-1) +" attempts.")
- print()
- print("The computer guessed the following numbers:" + str(table))#Little changes here
- break
- #Display menu
- def menu():
- print("1. You guess the number")
- print("2. You type a number and the computer guesses")
- print("3. Exit")
- #Display welcome message
- #Get users name
- print("Welcome to my Guess the Number Program!")
- print()
- name = input("What's your name? ")
- #Loop and implement users choosen option
- while True:
- print()
- print(name,"please choose an option from the menu below.")
- print()
- menu()
- option=int(input("What's your choice?: "))
- if (option==1):
- table=[]
- option1()
- elif(option==2):
- table=[]
- option2()
- elif(option==3):
- print()
- print("Thanks for playing the guess the number game" ,name,'!') #End Loop#End Program
- break
Add Comment
Please, Sign In to add comment