GeezerVisonThe World Wide Web, restored to 800 pixels of glory. |
| Home|About|Gaming cabinet|Archives|How it works|Starter sites |
GeezerVison is showing github.com — Beginner-Python-exercises/Exercise-25-Guessing-Game-Two.py at master · ihossai4/Beginner-Python-exercises Expand file tree/ Copy pathExercise-25-Guessing-Game-Two.pyMore file actions 100 lines (93 loc) · 2.05 KB / Copy pathExercise-25-Guessing-Game-Two.pyFile metadata and controls100 lines (93 loc) · 2.05 KB 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 ''' Exercise 25: Guessing Game Two In a previous exercise, we’ve written a program that “knows” a number and asks a user to guess it. This time, we’re going to do exactly the opposite. You, the user, will have in your head a number between 0 and 100. The program will guess a number, and you, the user, will say whether it is too high, too low, or your number. At the end of this exchange, your program should print out how many guesses it took to get your number. ''' # Solution def main(): import random guess_log = [random.randint(0, 101)] log_dict = {'lower':[0], 'higher':[100]} while True: print(guess_log[-1]) evaluate = input('Tell me my number is:\n1: too low\n2: too high\n3: Right\n') try: evaluate = int(evaluate) if evaluate == 1: log_dict['lower'].append(guess_log[-1]) guess_log.append(int((min(log_dict['higher']) + max(log_dict['lower']))/ 2 )) elif evaluate == 2: log_dict['higher'].append(guess_log[-1]) guess_log.append(int((min(log_dict['higher']) + max(log_dict['lower']))/ 2 )) else: print('Yeah!\n', guess_log) break except ValueError: print('Input error.') if __name__ == "__main__": main() # Test Part # >>> %Run test.py # 68 # Tell me my number is: # 1: too low # 2: too high # 3: Right2 # 34 # Tell me my number is: # 1: too low # 2: too high # 3: Right3 # Yeah! # [68, 34] # >>> %Run test.py # 40 # Tell me my number is: # 1: too low # 2: too high # 3: Right # 1 # 70 # Tell me my number is: # 1: too low # 2: too high # 3: Right # 1 # 85 # Tell me my number is: # 1: too low # 2: too high # 3: Right # 2 # 77 # Tell me my number is: # 1: too low # 2: too high # 3: Right # 1 # 81 # Tell me my number is: # 1: too low # 2: too high # 3: Right # 2 # 79 # Tell me my number is: # 1: too low # 2: too high # 3: Right # 2 # 78 # Tell me my number is: # 1: too low # 2: too high # 3: Right # 3 # Yeah! # [40, 70, 85, 77, 81, 79, 78] |
| You are visitor 01402792 — GeezerVison does not store the pages you look up. Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required. |