24.1k views
24 votes
Python exercise grade 10

Write a program that finds the largest in a series of numbers entered by the user. The program
must prompt the user to enter numbers one by one. When the user enters 0 or a negative
number, the program must display the largest nonnegative number entered:
Enter a number: 60
Enter a number: 38.3
Enter a number: 4.89
Enter a number: 100.62
Enter a number: 75.2295
Enter a number: 0
The largest number entered was 100.62
Notice that the numbers aren’t necessarily integers

User TheOni
by
3.7k points

2 Answers

12 votes

Final answer:

A Python program was provided that prompts the user for numbers and displays the largest positive number entered when the user inputs 0 or a negative number.

Step-by-step explanation:

The student's question is centered around creating a Python program that can find the largest number in a series of user-entered values. Here's a sample solution to the problem:

max_number = None
while True:
number = float(input("Enter a number: "))
if number <= 0:
break
if max_number is None or number > max_number:
max_number = number
if max_number is not None:
print("The largest number entered was", max_number)
else:
print("No positive numbers were entered.")

This program initializes a variable max_number to None, then continuously prompts the user to enter a number until a non-positive number is entered. It updates the max_number with the largest positive number entered by the user. When the loop ends, the largest number entered is displayed.

User Cesarsalazar
by
3.4k points
6 votes

nums = []

while True:

num = float(input("Enter a number: "))

if num <= 0:

break

nums.append(num)

print("The largest number entered was",max(nums))

I wrote my code in python 3.8. I hope this helps.

User Ken Birman
by
3.2k points