Write a program that prompts the user to enter positive integers (0 to stop). The…
- User
Write a program that prompts the user to enter positive integers (0 to stop). The program then should count and print the maximum integer and the minimum integer entered by the user.
- Assistant
Here is a Python program that prompts the user to enter positive integers and prints the maximum and minimum integers entered:
max_num = float('-inf') min_num = float('inf') while True: num = int(input("Enter a positive integer (0 to stop): ")) if num == 0: break if num > max_num: max_num = num if num < min_num: min_num = num print("Maximum integer entered:", max_num) print("Minimum integer entered:", min_num)In this program, we initialize variables
max_numandmin_numto negative and positive infinity, respectively. This ensures that any positive integer entered by the user will be higher thanmax_numand lower thanmin_numinitially.The program then enters a
whileloop that continues until the user enters 0. Inside the loop, it prompts the user to enter a positive integer and checks if it is higher thanmax_numor lower thanmin_num. If the condition is true, it updates the values ofmax_numandmin_num.After the loop breaks, the program prints the maximum and minimum integers entered by the user.