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. 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 say so. Similarly, you should tell them if their guess was too low.

Here is an example run of your final program, assuming that your float is 3.3312:

Enter a guess: 5.0
Too high!
Enter a guess: 3.0
Too low!
Enter a guess: 4.0
Too high!
Enter a guess: 3.5
Too high!
Enter a guess: 3.25
Too low!
Enter a guess: 3.33
Correct!
Use the loop and a half structure for your program. You should have a while loop that looks something like this:

while True:
...

if ...:
break

...
Be careful - you may end up with an infinite loop in your program if you don’t break correctly! If you run a program with an infinite loop in it, the page might freeze and you might have to refresh it to continue.

1 answer

Certainly! Below is a Python program that implements the guessing game for a specific float, matching your requirements:

def guessing_game(target_float):
    print("Welcome to the guessing game!")
    print("Try to guess the float (accurate to within 2 decimal places).")
    
    while True:
        try:
            # Prompt the user for their guess
            guess = float(input("Enter a guess: "))
            
            # Compare the guessed value with the target value rounded to 2 decimal places
            if round(guess, 2) == round(target_float, 2):
                print("Correct!")
                break  # Exit the loop if the guess is correct
            elif guess > target_float:
                print("Too high!")
            else:
                print("Too low!")
        except ValueError:
            print("Invalid input. Please enter a floating point number.")

if __name__ == "__main__":
    # You can change this float value to test different scenarios
    target_number = 3.3312
    guessing_game(target_number)

Explanation of the Code:

  1. We define a function guessing_game that takes a floating-point number (target_float) as an argument.
  2. Inside the function, we use a while True: loop for continuous guessing.
  3. We prompt the user for their guess and attempt to convert it to a float.
  4. We check if the rounded guess (to 2 decimal places) is equal to the rounded target number:
    • If they are equal, print "Correct!" and break out of the loop.
    • If the guess is higher than the target, print "Too high!".
    • If the guess is lower than the target, print "Too low!".
  5. We handle invalid inputs with a try-except block to catch ValueError exceptions if the user does not enter a valid float.
  6. The game starts in the if __name__ == "__main__": block with a predefined target float (which you can modify as needed).

To run the program:

  • Simply copy the code into a Python environment and execute it. The program will prompt you to guess the target float until you guess it correctly or provide an invalid input.
Similar Questions
    1. answers icon 1 answer
  1. QuestionMaria is creating a program where the user will enter their name to begin. What kind of variable should be used for the
    1. answers icon 1 answer
    1. answers icon 1 answer
    1. answers icon 1 answer
more similar questions