Reputation: 11
num_1 = input("Please enter first number: ")
num_2 = input("Please enter second number: ")
print("The numbers you have chosen are",num_1,"and",num_2)
while num_1 or num_2 > 0:
if num_1 < num_2:
print("First number entered is greater.")
elif num_1 > num_2:
print("Second number entered is greater.")
else:
print("Both numbers entered are equal.")
print("Program terminated...")
Upvotes: 0
Views: 1150
Reputation:
Try this:
num1 = int(input('Enter first number: '))
num2 = int(input('Enter second number: '))
if num1 == num2:
print('Both are equal.')
elif num1 > num2:
print('Number 1 is greater.')
elif num1 < num2:
print('Number 2 is greater.')
print('Program terminated.')
Always remember that when you want a number from the user, you need to type int(input([prompt])) instead of input([prompt]).
You don't need a 'while' loop for this program but if you really want one then you can do this:
num1 = int(input('Enter first number: '))
num2 = int(input('Enter second number: '))
while True:
if num1 == num2:
print('Both are equal.')
break
elif num1 > num2:
print('Number 1 is greater.')
break
elif num1 < num2:
print('Number 2 is greater.')
break
print('Program terminated.')
'break' tells the loop to break and continue with the program.
Upvotes: 0
Reputation: 113905
The condition on your while loop is improperly expressed. What you mean to say is "as long as at least one number is greater than 0".
However, what you have expressed is this:
while num_1 or num_2 > 0
Python reads this as:
while ((num_1) or (num_2 > 0)):
The condition therefore asks about the boolean value of num_1
, which evaluates to False
ONLY if num_1
is 0
. It also asks whether num_2
is greater than 0
and takes the OR
of both of those booleans.
This roughly translates to:
while (num_1 is not 0 or num_2 is larger than 0)
What you are looking for is
while (num_1 is larger than 0 or num_2 is larger than 0)
... which can be written as:
while num_1 > 0 or num_2 > 0
Further, you never redefine num_1
and num_2
in your while loop, which is why it keeps going over your loop without stopping. You could fix that as follows:
while :
# your if statements
num_1 = input("Please enter first number: ")
num_2 = input("Please enter second number: ")
print("The numbers you have chosen are",num_1,"and",num_2)
This keeps asking the user for input until they enter two non-positive numbers
Upvotes: 1
Reputation: 105
it never exits the while loop. Replace the while statement with another if
Upvotes: 0