Benedictus

random_int.py

Dec 18th, 2019
358
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.83 KB | None | 0 0
  1. #!/usr/bin/env python3
  2. #Guess the number week 7
  3. #This program is a number guessing game
  4. #Amanda Rhodes
  5. #12/14/19
  6. import random
  7. #Display Welcome Message and ask for a number
  8.  
  9. table = [] #HERE
  10. d = 0 #HERE TOO
  11. count = 1
  12. my_number = random.randint(1, 10)
  13. #Random number, loop while true
  14. #User Guesses a number
  15.  
  16. def option1():
  17.     count = 1
  18.     you_guessed=[]
  19.     my_number = random.randint(1, 10)
  20.     while True:
  21.         print()
  22.         try:
  23.             user_guess = int(input("Guess a number between 1 and 10: "))
  24.             print()
  25.         except ValueError:
  26.             print()
  27.             print("Numbers only please!")
  28.             continue
  29.         if user_guess < 1 or user_guess > 10:
  30.             print("Sorry, entry must be a number between 1 and 10.")
  31.             continue
  32.         if user_guess in (0, 11):
  33.             break
  34. #tell user if guess is correct or not
  35. #If correct tell user how many attempts
  36. #List all numbers guessed
  37.         if user_guess < my_number:
  38.             print("Too Low!")
  39.             count=count+1
  40.             you_guessed.append(user_guess)
  41.         elif user_guess > my_number:
  42.             print("Too High!")
  43.             count=count+1
  44.             you_guessed.append(user_guess)
  45.         elif user_guess == my_number:
  46.             print("You Guessed it! It took you " + str(count) +" attempts.")
  47.             print()
  48.             you_guessed.append(user_guess)
  49.             print("You picked the following numbers:" + str(you_guessed))
  50. #break out of loop
  51.             break
  52.  
  53. #my changes
  54. #DEFINED RANDOM FUNCTION,SO WE CAN CALL IT ANYTIME
  55. def up():
  56.     randnum = random.randint(1,10)
  57.     return randnum
  58.    
  59. #My changes  
  60. #DEFINED A LOOK-UP FUNCTION TO VERIFY IF ALREADY GUESSED
  61. #NUMBER EXISTS
  62. def lookup():
  63.     global d
  64.     net = up()
  65.     if net in table:
  66.         lookup()
  67.     elif net not in table:
  68.         table.append(net)
  69.         d = len(table)
  70.         net = net
  71.     return net
  72.  
  73.  
  74. #computer guesses a number
  75. #Display guessed numbers and number of attempts
  76. def option2():
  77.     global table #HERE ALSO
  78.     print()
  79.     computer_guess = int(input("Please enter a number between 1 and 10 for the computer to guess: "))
  80.     print()
  81.     count=1
  82.     computer_guessed=[]
  83.     while True:
  84.         print()
  85.         print()
  86.         lookup() #HERE ALSO
  87.         randomval = 0 #HERE TOO
  88.         computer_guessed.append(randomval)
  89.         for sip in range(0,len(table)):#HERE ALSO
  90.             randomval =table[sip]
  91.         if randomval< computer_guess:
  92.             print("The computer guessed ", [randomval] ," which is too low")
  93.            
  94.             continue
  95.         elif randomval> computer_guess:
  96.             print("The computer guessed ", [randomval]," which is too high")
  97.             #count=count+1
  98.             continue
  99.         else:
  100.             computer_guess ==randomval
  101.             print("The computer guessed ", randomval," which is correct. It took " + str(len(table)-1) +" attempts.")
  102.             print()
  103.             print("The computer guessed the following numbers:" + str(table))#Little changes here
  104.             break
  105.  
  106. #Display menu
  107. def menu():
  108.     print("1. You guess the number")
  109.     print("2. You type a number and the computer guesses")
  110.     print("3. Exit")
  111.    
  112. #Display welcome message
  113. #Get users name
  114. print("Welcome to my Guess the Number Program!")
  115. print()
  116. name = input("What's your name? ")
  117.  
  118. #Loop and implement users choosen option
  119. while True:    
  120.     print()
  121.     print(name,"please choose an option from the menu below.")
  122.     print()
  123.     menu()
  124.     option=int(input("What's your choice?: "))
  125.     if (option==1):
  126.         table=[]
  127.         option1()
  128.     elif(option==2):
  129.         table=[]
  130.         option2()
  131.     elif(option==3):
  132.         print()
  133.         print("Thanks for playing the guess the number game" ,name,'!') #End Loop#End Program
  134.         break
Add Comment
Please, Sign In to add comment