Question

5.2 Write a program that repeatedly prompts a user for integer numbers until the user enters...

5.2 Write a program that repeatedly prompts a user for integer numbers until the user enters 'done'. Once 'done' is entered, print out the largest and smallest of the numbers. If the user enters anything other than a valid number catch it with a try/except and put out an appropriate message and ignore the number. Enter 7, 2, bob, 10, and 4 and match the output below. ​ 1 largest = None 2 smallest = None 3 while True: 4 num = input("Enter a number: ") 5 if num == "done" : break 6 print(num) 7 8 print("Maximum", largest)

0 0
Add a comment Improve this question Transcribed image text
Answer #1
maxVal = None
minVal = None
while(True):
    num = input("Enter a number: ")
    if num == "done":
        break
    try:
        val = int(num)
        if(maxVal == None):
            maxVal = val
        if(minVal == None):
            minVal = val
        if(maxVal < val):
            maxVal = val
        if(minVal > val):
            minVal = val
    except Exception as e:
        print("Error: Please enter only valid integers")
print("Maximum:",maxVal)
print("Minimum:",minVal)

Output:

Enter a number: 7 Enter a number: 2 Enter a number: bob Error: Please enter only valid integers Enter a number: 10 Enter a nu

Please upvote the solution if it helped. Thanks!

Add a comment
Know the answer?
Add Answer to:
5.2 Write a program that repeatedly prompts a user for integer numbers until the user enters...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Summary: Write a C program that prompts the user to enter 2 positive integer numbers, display...

    Summary: Write a C program that prompts the user to enter 2 positive integer numbers, display the numbers in 3 formats. Then check, whether the larger of the 2 is evenly divisible by the smaller. Detail: Write a complete C program, more complex than the typical "hello world" program. Prompt the user to enter 2 integer numbers that are not negative. After either entry, display that number again. Print the smaller in hexadecimal, in decimal, and in octal format. Include...

  • Write a program which: - prompts the user for 2 numerical inputs - stores their two...

    Write a program which: - prompts the user for 2 numerical inputs - stores their two inputs in variables as floats - Use a while in loop to ask a user for a valid operation: (if the user's input operation isn't one of those listed above, instead print a message telling them so, and continue asking for the valid operation) - prompts the user for an arithmetic operation ( + - * / %) - stores the user's input as...

  • Using a Python environment, complete two small Python programs - Write a function which repeatedly reads...

    Using a Python environment, complete two small Python programs - Write a function which repeatedly reads numbers until the user enters “done” Once “done” is entered, print out the sum, average, maximum, and minimum of the values entered If the user enters anything other than a number, detect their mistake using try and except and print an error message and skip to the next number. 2. Write another function so that it draws a sideways tree pointing right; for example,...

  • C++ Problem #1 Write a program that prompts user to enter an integer up to 1000...

    C++ Problem #1 Write a program that prompts user to enter an integer up to 1000 and displays back a message about the number of digits in this number and if number is odd or even. For example, if user enters 93 message back should be "93 has 2 digits and is odd".

  • 1. Write a program that prompts the user to enter three integers and display the integers...

    1. Write a program that prompts the user to enter three integers and display the integers in non-decreasing order. You can assume that all numbers are valid. For example: Input Result 140 -5 10 Enter a number: Enter a number: Enter a number: -5, 10, 140 import java.util.Scanner; public class Lab01 { public static void main(String[] args) { Scanner input = new Scanner(System.in);    } } ---------------------------------------------------------------------------------------------------------------------------- 2. Write a program that repeatedly prompts the user for integer values from...

  • Write a program that (in the main program) prompts the user for the (integer) model number...

    Write a program that (in the main program) prompts the user for the (integer) model number of their car. The main function should then call the function check defective which should • take in the model number of the car • check if the model number is 119, 179, 221, 780, or anything between 189 and 195, inclusive, which indicates the car is defective • print a message indicating whether or not the car is defective Note: this is should...

  • Write code to repeatedly ask the user for a number. If the number is positive and...

    Write code to repeatedly ask the user for a number. If the number is positive and even, print out a happy message. If the number is positive and odd, print out a sad message. If the number is negative, print out an angry message If they enter zero, stop asking If they enter something that isn't an integer, print out a message telling them they are an idiot, but keep looping

  • please help me by matlab 5.2) Develop a program that prompts the user for three numbers....

    please help me by matlab 5.2) Develop a program that prompts the user for three numbers. Once received, print the multiplication of the first two numbers. Use the third number as the quantity of decimal places to print. 5.3) Develop a program that prompts the user for two numbers and a calculation operator (+,-/or *). Once received, use appropriate IF STATEMENTS to complete the calculation guided by the following rules: Addition Subtraction Division Multiplication Print to output to three decimal...

  • C++ Write a program that prompts the user to enter integers or a sentinel to stop....

    C++ Write a program that prompts the user to enter integers or a sentinel to stop. The program prints the highest and lowest numbers entered, the sum and the average. DO NOT USE ARRAYS, only variables and loops. Write a program the prompts the user to input a positive integer. Keep asking the user until a positive integer is entered. Once a positive integer is entered, print a message if the integer is prime or not. (NOTE: A number is...

  • c++ please (1) Write a program that prompts the user to enter an integer value N...

    c++ please (1) Write a program that prompts the user to enter an integer value N which will rpresent the parameter to be sent to a function. Use the format below. Then write a function named CubicRoot The function will receive the parameter N and return'its cubic value. In the main program print the message: (30 Points) Enter an integer N: [. ..1 The cubic root of I.. ] is 2 update the code om part and write another function...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT