• API
• FAQ
• Tools
• Archive
daily pastebin goal
50%
SHARE
TWEET

# Untitled

a guest Dec 7th, 2017 54 Never
ENDING IN00days00hours00mins00secs
1. #To expedite things, I simply had the computer determine if its answer was correct instead of prompting the user.
2. #If desired, the if statements in the function Guesser() can be changed to prompt the user for a right/wrong response.
3.
4. def GetNumber():
5.     global SecretNumber
6.     try:
7.         SecretNumber = int(input("\nEnter a secret number between 1 and 100: "))
8.     except ValueError:
9.         print("That's not an integer.  Try again.")
10.         GetNumber()
11.     if SecretNumber < 1 or SecretNumber > 100:
12.         print("That's not a number between 1 and 100.  Try again.")
13.         GetNumber()
14.
15. GetNumber()
16.
17. NumberList = list(range(1,101))
18.
19. IndexStart = 0
20. IndexEnd = len(NumberList)
21. GuessCount = 0
22.
23. def Guesser():
24.     global IndexStart
25.     global IndexEnd
26.     global GuessCount
27.     GuessCount += 1
28.     IndexMidway = (IndexEnd + IndexStart) // 2            #Using // instead of / to ensure we get a whole number.
29.     print("\nMy guess is",IndexMidway)
30.     if IndexMidway == SecretNumber:
31.         print("I'm a winner!  Your secret number is",SecretNumber)
32.         print("It took me",GuessCount,"guesses!")
33.         exit()
34.     elif SecretNumber < IndexMidway:
35.         print("Your secret number is LOWER than",IndexMidway)
36.         IndexEnd = IndexMidway
37.         print("I will try again, guessing numbers between",IndexStart,"and",IndexEnd)
38.         Guesser()
39.     elif SecretNumber > IndexMidway:
40.         print("Your secret number is HIGHER than",IndexMidway)
41.         IndexStart = IndexMidway
42.         print("I will try again, guessing numbers between",IndexStart,"and",IndexEnd)
43.         Guesser()
44.
45.
46. Guesser()
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy.

Top