0
votes

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.

3
This isn't really appropriate for Stack Overflow. If you have a specific question about why some code isn't working, you can ask a new question, but we're not going to do your homework.Morgan Thrapp
On lines 1-3 you ask for a number each time, but then line 2 overwrites the input from line 1, and line 3 overwrites the input from line 2 ... so you are not even checking the result of the first two inputs, they are just discarded. You should read about looping, i.e. while or for.Dan Lowe
@DanLowe That should basically be an (the) answer instead of a comment. As far as this question goes, it's the best answer since it solves the immediate problem without either invoking too-advanced concepts or propsoing fixes in a way that completes the assignment.Two-Bit Alchemist
@DanLowe Thank you for pointing me in the right direction, that helps with my problem! I appreciate it!B. Trev
@Two-BitAlchemist fair enough - posted again as an answer. I hadn't done that before because I usually don't answer "homework" questions, but I see where you're going.Dan Lowe

3 Answers

2
votes

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.

0
votes

Use the properties of the numbers...

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

Try Something like this

if num1< 0 or num2 <0 or num3 < 0:
  print ('no')

elif num1 > 0 or num2 > 0 or num3 > 0:
  print ('yes')
0
votes

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")