Exercise 3
Write a Python algorithm which asks the user to enter two numbers 'a' and 'b' and display their maximum without using the max() function.
Solution
# ask the user to enter two numbers 'a' and 'b' a = float(input("Enter the first number: ")) b = float(input("Enter the second number: ")) # we compare the values of 'a' and 'b' if a > b: print("The maximum of", a, "and", b, "is", a) else: print("The maximum of", a, "and", b, "is", b)
In this program:
- We ask the user: to enter two numbers a and b using the input() function.
- The numbers 'a' and 'b': are stored as floats in the variables a and b.
- We use the if statement: to compare the values of 'a' and 'b'.
- If a is greater than b: the program prints a message indicating that the maximum of the two numbers is 'a'.
- Otherwise: the program prints a message indicating that the maximum of the two numbers is 'b'.
Younes Derfoufi
my-courses.net
[…] Exercise 3 || Solution […]