159k views
3 votes
For this assignment, you will first prompt the user and read in two numbers from them. You need to check and make sure that both numbers are equal to or greater than 0 (can use an if or if/else statement for this). If so, multiply the two numbers together and print the new value to the screen.

If the user enters a number lower than 0, remind the user that they need to enter a number greater than or equal to 0 and exit the program.
Sample Runs
Sample Program Run (user input is underlined)
Please type in two numbers: 100 250
100 * 250 = 25000
Exiting program.
Please type in two numbers: -1 27
Please only enter numbers >= 0.
Exiting program.
(Here's an alternative way to do the same thing:)
Please type in one number: 100
Please type in a second number: 250
100 * 250 = 25000
Exiting program.
Please type in one number: -1
Please type in a second number: 27
Please only enter numbers >= 0.
Exiting program.

User Eko
by
5.0k points

1 Answer

2 votes

Answer:

number1 = int(input("Please type in one number: "))

number2 = int(input("Please type in a second number: "))

if number1 >=0 and number2 >=0:

print(str(number1) + " * " + str(number2) + " = " + str(number1 * number2))

else:

print("Please only enter numbers >= 0.")

print("Exiting program.")

Using a Loop:

while True:

number1 = int(input("Please type in one number: "))

number2 = int(input("Please type in a second number: "))

if number1 >=0 and number2 >=0:

print(str(number1) + " * " + str(number2) + " = " + str(number1 * number2))

print("Exiting program.")

else:

print("Please only enter numbers >= 0.")

print("Exiting program.")

break

Step-by-step explanation:

*The code is in Python.

Ask the user to enter two numbers

Check if they are both greater than or equal to 0. If they are, print their multiplication. Otherwise, print the remainder message

Print a message says "exiting program"

Note: Since I was not sure if the program keeps going until the user enters a negative value, I wrote two versions of the program. The one that uses the loop continues until the user enters a negative value.

User Hirabayashi Taro
by
4.8k points