0
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...")
Martijn Pieters
1.1m326 gold badges4.2k silver badges3.5k bronze badges
asked Apr 21, 2014 at 18:17
2
  • 6
    Why use a loop at all here? Nothing changes in the loop, so the conditions for the loop also don't change. You have an endless loop with no apparent goal. Commented Apr 21, 2014 at 18:19
  • Exactly! It's like asking why while True: never stops. You either have to have a condition that eventually becomes false or a break inside the loop to end it. And it should be mentioned that you meant to say while num_1 > 0 and num_2 > 0:, although this will not solve your main problem. Commented Apr 21, 2014 at 18:20

3 Answers 3

1

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

answered Apr 21, 2014 at 18:24
Sign up to request clarification or add additional context in comments.

Comments

0

it never exits the while loop. Replace the while statement with another if

answered Apr 21, 2014 at 18:22

Comments

0

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.

answered Apr 21, 2014 at 18:45

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.