B. Trev
B. Trev

Reputation: 13

If-Else Statement-Positive and Negative Integers

I am just starting my first computer science class and have a question! Here are the exact questions from my class:

"Write a complete python program that allows the user to input 3 integers and outputs yes if all three of the integers are positive and otherwise outputs no. For example inputs of 1,-1,5. Would output no."

"Write a complete python program that allows the user to input 3 integers and outputs yes if any of three of the integers is positive and otherwise outputs no. For example inputs of 1,-1,5. Would output yes."

I started using the if-else statement(hopefully I am on the right track with that), but I am having issues with my output.

num = int(input("Enter a number: "))
num = int(input("Enter a number: "))
num = int(input("Enter a number: "))
if num > 0:
   print("YES")
else:
   print("NO")

I have this, but I am not sure where to go with this to get the desired answers. I do not know if I need to add an elif or if I need to tweak something else.

Upvotes: 0

Views: 10609

Answers (2)

Dan Lowe
Dan Lowe

Reputation: 56508

On the first 3 lines, you collect a number, but always into the same variable (num). Since you don't look at the value of num in between, the first two collected values are discarded.

You should look into using a loop, e.g. for n in range(3):

for n in range(3):
    num = int(input("Enter a number: "))
    if num > 0:
        print("YES")
    else:
        print("NO")

Upvotes: 0

Michael S Priz
Michael S Priz

Reputation: 1126

You probably want to create three separate variables like this:

num1 = int(input("Enter number 1: "))
num2 = int(input("Enter number 2: "))
num3 = int(input("Enter number 3: "))

In your code you only keep the value of the last number, as you are always writing to the same variable name :)

From here using an if else statement is the correct idea! You should give it a try :) If you get stuck try looking up and and or keywords in python.

Upvotes: 2

Related Questions