Write a program that makes the user guess a particular float. Now, recall that you should not compare floats to determine if they are exactly equal. In this program, the user wins if their guess is accurate to within 2 decimal places. (Note: The user should be able to enter a number with more than 2 decimal places and the program should check if that value rounded to 2 decimal places matches the number to guess to 2 decimal places.) Because of this, you will have to use round in your comparison of their guess to your number. If the user’s guess was too high, you should print to the console Too high!. Similarly, if the guess was too low, you should print to the console Too low!. Finally, if the user guesses the correct number, you should print Correct! to the console. Ensuring you print these specific texts is important for the exercise grader.
Too high!
Too low!
Correct!
Here’s a simple Python program that implements the guess-the-float game as you described:
import random
# Generate a random float between 1.00 and 100.00
number_to_guess = round(random.uniform(1.00, 100.00), 2)
# Ask the user to guess the number
user_guess = float(input("Guess a float number between 1.00 and 100.00: "))
# Compare rounded guesses
if round(user_guess, 2) > number_to_guess:
print("Too high!")
elif round(user_guess, 2) < number_to_guess:
print("Too low!")
else:
print("Correct!")
### Explanation:
1. Random Float Generation: The program generates a random float between 1.00 and 100.00 rounded to 2 decimal places.
2. User Input: It prompts the user to input their guess.
3. Comparison:
– It rounds both the user’s guess and the number to guess to 2 decimal places before comparing them.
– Depending on the comparison, it prints the appropriate message: “Too high!”, “Too low!”, or “Correct!”.
Make sure to test the program to see how it works! If you need more help or further explanations, feel free to ask!