Question

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 a string
- stores the result of the specfied operation with the specified
numbers
- if the inputs are invalid for the operation (i.e. dividing by
0) print a message informing the user and Use a while in loop to ask a user for a
valid input and then calculate and print out the new result
- if the inputs were valid, prints out the resulting equation
- for instance, if the user enters '2' '3' and '+' then "2.0 +
3.0 = 5.0" should be printed

please use python 3

0 0
Add a comment Improve this question Transcribed image text
Answer #1

a=input("Enter num1:")                      #num1 input
b=input("Enter num2:")                      #num2 input
n1=float(a)                                 #converting to float
n2=float(b)                                 #converting to float
res=0                                       #result variable
while(True):                                #infinite loop until valid operation is performed
    print("Enter + or - or * or / or % :") #user prompt
    ip=str(input("Enter operation:"))       #input from user to perform an operation
    if(ip=='+'):
        res=n1+n2
        break
    elif(ip=='-'):
        res=n1-n2
        break
    elif(ip=='*'):
        res=n1*n2
        break
    elif(ip=='/' and n2!=0):
        res=n1//n2
        break
    elif(ip=='%' and n2!=0):
        res=n1%n2
        break
print(res)                              #printing result

Add a comment
Know the answer?
Add Answer to:
Write a program which: - prompts the user for 2 numerical inputs - stores their two...
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
  • Write a code segment that prompts the user to enter a double number between (5.55 and...

    Write a code segment that prompts the user to enter a double number between (5.55 and 123.3) inclusive. If the input is invalid, print an error message and loop until the input is valid. Code in c++

  • Write a program that prompts the user for two inputs: a divisor and a dividend that...

    Write a program that prompts the user for two inputs: a divisor and a dividend that can handle the exceptions of input failure (non-integer input for both dividend and divisor) and dividing by 0 (divisor only). Use try-catch statements and throw statements.

  • PYTHON CODING Create a program that prompts the user twice. The first prompt should ask for...

    PYTHON CODING Create a program that prompts the user twice. The first prompt should ask for the user's most challenging course at Wilmington University. The second prompt should ask for the user's second most challenging course. The red boxes indicate the input from the user. Your program should respond with two copies of an enthusiastic comment about both courses. Be sure to include the input provided by the user to display the message. There are many ways to display a...

  • 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...

  • Write a program that prompts the user to enter a person's birth date in numeric form...

    Write a program that prompts the user to enter a person's birth date in numeric form (e.g. 8-27-1980) and outputs the date of birth in the format of month day, year (e.g. August 27, 1980). Your program must work for three types of exceptions - invalid day, invalid month and invalid year (year must be between 1915 and 2015). Make these as class driven exceptions. Use one try-catch block with separate catches for the exception classes to handle errors for...

  • JAVA 2. (8 points) Write a code segment which prompts the user to enter the price...

    JAVA 2. (8 points) Write a code segment which prompts the user to enter the price of a tour and receives input from the user. A valid tour price is between $52.95 and $259.95, inclusive. Your program should continue to repeat until a valid tour price has been entered. If the user enters an invalid price, display a message describing why the input is invalid. You may assume that the user only enters a real number. The user will not...

  • Create a script that prompts the user for which computer they want to ping. The user...

    Create a script that prompts the user for which computer they want to ping. The user can input SWS, DC, or DM –if they input anything else make them reenter their input. Depending upon the valid user input, print out a message which option was selected then ping the correct IP-Address. Paste code here:

  • JAVA: (15 marks) Write a program that creates an integer array with 50 random values, prompts...

    JAVA: (15 marks) Write a program that creates an integer array with 50 random values, prompts the user to enter the index of an element in the array between 0 and 49, then displays the corresponding element value. If the specified index is out of bounds, display an error message (e.g. "Out of Bounds") and ask the user to enter another index. Use a while loop that will keep prompting the user until a valid input is received. To handle...

  • C++ Write a program that prompts the user to enter two positive integers: num1 and num2....

    C++ Write a program that prompts the user to enter two positive integers: num1 and num2. - Validate that num1 is less than num2 and that both numbers are positive. If any of these conditions are not met, allow the user to re-enter num1 and num2 until the input is determined valid. - For all integers from num1 through num2, print the word keyboard if the current integer is divisible by 2 and print the word mouse if the current...

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