""" Felix Muzny DS 2000 Lecture 5 Write a program, hotcold.py, that randomly picks a number between 1 and 10 then has the user guess a number. If the user is correct, congratulate them. If the user is incorrect, prompt them to guess again. Each time the user guesses tell them "warmer" if they are getting closer to the secret number and "colder" if they are getting farther away. """ # import random module import random # for plotting import matplotlib.pyplot as plt def main(): # generate a random int [1, 10] (inclusive) num = random.randint(1, 10) print("random num:", num) # 1 set initial values of important variables guess = int(input("guess? ")) # what was the previous guess prev_guess = 0 guess_count = 0 # 2 test to see if the loop should repeat while guess != num: # plot the guess plt.plot(guess_count, guess, "o") # circle # absolute value curr_dist = abs(guess - num) prev_dist = abs(prev_guess - num) # tell the user if they are warmer or colder if prev_dist > curr_dist: print("warmer") else: print("colder") # 3) update values, in particular # guess -- which will stop the program prev_guess = guess guess = int(input("new guess? ")) guess_count += 1 # plot the final (correct) guess plt.plot(guess_count, guess, "s") # square # finish setting up the plot plt.xlabel("Guess number") plt.ylabel("Guess value") plt.title("Guesses over time in this thrilling guessing game") plt.show() # you KNOW guess == num at this point print("you won!") main()