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.
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.
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")
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')
Related
I created some code where I try to find elements in a list given by the user using the in operator.
productNums = []
nums = int(input("How many numbers should be in your list?"))
while nums > 0:
productNums.append(input("Add a number: "))
nums -= 1
numFind = int(input("What number are you trying to find? "))
if numFind in productNums:
print("This product number is in the list")
elif numFind not in productNums:
print("Ahh, sorry. This product number is NOT in this list!")
else:
print("Huh, you're confusing me now, check your numbers pal!")
When I run this, I get the outcome that the number is not in this list, even though it is in it. Can someone tell me what I am doing wrong?
As people said above, just small issue with you integers and strings.
This is your code working:
Be careful with the indentention, initially was a mess.
productNums = []
nums = int(input("How many numbers should be in your list?"))
while nums > 0:
productNums.append(int(input("Add a number: ")))
nums -= 1
numFind = int(input("What number are you trying to find? "))
if numFind in productNums:
print("This product number is in the list")
elif numFind not in productNums:
print("Ahh, sorry. This product number is NOT in this list!")
else:
print("Huh, you're confusing me now, check your numbers pal!")
I believe its a missing conversion when u append, its appended as a string and u search for an int
Because the productNums list contains string values, not integer, so you always compare int with str variable and obtain the same result. Set productNums elements to int and it will work properly.
productNums = []
nums = int(input("How many numbers should be in your list?"))
while nums > 0:
productNums.append(int(input("Add a number: ")))
nums -= 1
numFind = int(input("What number are you trying to find? "))
if numFind in productNums:
print("This product number is in the list")
elif numFind not in productNums:
print("Ahh, sorry. This product number is NOT in this list!")
else:
print("Huh, you're confusing me now, check your numbers pal!")
You correctly cast nums and numFind to int, but you didn't do so for the input values in the while loop — so productNums is a list of single-character strings.
This change will fix it:
productNums.append(int(input("Add a number: ")))
The problem is that you are trying to find an int in a list of strings.
When you wrote the code productNums.append(input("Add a number: ")), after the user enters the code, the number would be appended into the productNums list as a string.
Further on, the numFind = int(input("What number are you trying to find? ")) makes the user enter a number. this time, the input is converted into an int. So when the line of code if numFind in productNums: runs, it sees the if as the user trying to find an int in a list of strings.
In short, you should either change the productNums.append(input("Add a number: ")) code into productNums.append(int(input("Add a number: "))), or change the line of code numFind = int(input("What number are you trying to find? ")) into numFind = input("What number are you trying to find? ").
how can i print the sum of all the numbers between any two given numbers in python.
i am not allowed to use functions like sum(). i can only use while or for loop.
i have a code which does the job but the problem is it also prints result every time after adding two numbers but i only want it to print final sum of all the numbers.
Here's the code:
...
a = int(input("Please enter a number"))
b = int(input("Please enter a number"))
n = 0
for x in range(a+1,b):
n+=x
print(n)
...
thanks
There is an Indentation Error just check the line below the for loop and give the proper indentation. You can refer to my code
a = int(input("Please enter a number: "))
b = int(input("Please enter a number: "))
n = 0
for x in range(a+1,b):
n+=x
print(n)
I am learning python. I need to know, is this also valid way to get an output?
Following code should give me highest user input( highest number among the four numbers)
num1 = int(input("Enter number 1: ")) #user 1
num2 = int(input("Enter number 2: ")) #user 2
num3 = int(input("Enter number 3: ")) #user 3
num4 = int(input("Enter number 4: ")) #user 4
allNum = {num1, num2, num3, num4}
print("All numbers: ",allNum)
if(num1 > num2 and num1 > num4) :
print("User 1's number is greater")
elif(num2 > num3) :
print("User 2's number is greater")
elif(num3 > num4) :
print("User 3's number is greater")
elif(num4 > num2) :
print("User 4's number is greater")
else :
print("done")
With your current method of directly comparing each number, you would do something like this:
if num1 > num2 and num1 > num3 and num1 > num4:
...
elif num2 > num1 and num2 > num3 and num2 > num4:
...
...
Luckily, there is an easier way! You can use Python's built-in max() function, which returns the maximum value of a sequence. That would look something like this:
maximum = max(num1, num2, num3, num4)
if num1 == maximum:
...
elif num2 == maximum:
...
...
The above method works fine, but what if you wanted to use 5 numbers? What about 6? 20? The more numbers you add, the more messy it gets. Each additional number of numbers, you would add another elif statement, which gets tedious. So I would also recommend using some loops and a dictionary for gathering, storing, and comparing the user input.
>>> nums = {}
>>> for i in range(4):
nums[i+1] = int(input(f"Enter number {i+1}: "))
Enter number 1: 4
Enter number 2: 2
Enter number 3: 8
Enter number 4: 10
>>> print("All numbers:", list(nums.values()))
All numbers: [4, 2, 8, 10]
>>> for k, v in nums.items():
if v == max(nums.values()):
print(f"Number {k} is greater")
break
Number 4 is greater
>>>
Trying to keep it as simple as possible for you to understand
num1 = int(input("Enter number 1: "))
num2 = int(input("Enter number 2: "))
num3 = int(input("Enter number 3: "))
num4 = int(input("Enter number 4: "))
#Store all numbers in list.
allNum = [num1, num2, num3, num4]
#Get the max value of all numbers in that list
maxNum = max(allNum)
#To get the position of this value, use list.index() function
maxIndex = allNum.index(max(allNum))
#Lets say your numbers are [3,5,4,1]. .index() would give you "1" because the index starts from 0 to length of list -1.
#In this case, it would be 0-3. So, if you want `Number 2` is greater instead, then you just add 1 to the maxIndex.
print("Greatest number is number "+str(maxIndex+1))
You can index and max to solve this.
a = []
for i in range(4):
a.append(int(input(f"Enter number {i+1}: ")))
print(f"Number {a.index(max(a)) + 1} is greater")
This is the code i wrote .. I'm just learning python
n1 = int(input("Enter 1st Number: "))
n2 = int(input("Enter 2nd Number: "))
n3 = int(input("Enter 3rd Number: "))
n4 = int(input("Enter 4th Number: "))
if (n1>n2) and (n1>n3) and (n1>n4):
print("n1 is Greater")
elif (n2>n1) and (n2>n3) and (n2>n4):
print("n2 is Greater")
elif (n3>n2) and (n3>n1) and (n3>n4):
print("n3 is Greater")
else:
print(n4,"is Greater")
Apologies if the question to this is worded a bit poorly, but I can't seem to find help on this exercise anywhere. I am writing a basic Python script which sums two numbers together, but if both numbers inputted are the same the sum will not be calculated.
while True:
print('Please enter a number ')
num1 = input()
print('Please enter a second number ')
num2 = input()
if num1 == num2:
print('Bingo equal numbers!')
continue
elif num1 == num2:
print('It was fun calculating for you!')
break
print('The sum of both numbers is = ' + str(int(num1) + int(num2)))
break
If both numbers are equal I want the script to loop back once more and if the numbers inputted are equal again I want the program to end. With the code I have provided the issue I am having is that when I enter two equal numbers it keeps constantly looping until I enter two different numbers.
You would likely want to have a variable keeping track of the number of times that the numbers were matching. Then do something if that counter (keeping track of the matching) is over a certain threshold. Try something like
matches = 0
while True:
num1 = input('Please enter a number: ')
num2 = input('Please enter a second number: ')
if num1 == num2 and matches < 1:
matches += 1
print('Bingo equal numbers!')
continue
elif num1 == num2:
print('It was fun calculating for you!')
break
print('The sum of both numbers is = ' + str(int(num1) + int(num2)))
break
You can add give input code again inside first if statement or use some other dummy variable for loop so that you can break the loop, for e.g. use while j == 0 and increase it j += 1when you are inside the first if statement
continue skips the execution of everything else in the loop. I don't see it much useful in your example. If you want to print the sum then just remove it.
How continue works can be demonstrated by this sample (taken from python docs)
for num in range(2, 10):
if num % 2 == 0:
print("Found an even number", num)
continue
print("Found a number", num)
Result
Found an even number 2
Found a number 3
Found an even number 4
Found a number 5
Found an even number 6
Found a number 7
Found an even number 8
Found a number 9
I am new to coding and I'm trying to figure a simple code. The user will input a number, that must be an integer greater than 0, then asked to enter a second integer, greater than the previous one. once the second value is entered the two inputs should be displayed as well as the even and odd numbers in-between the two inputs. Currently, the code I have doesn't distinguish the value of the second input, allowing it to be lesser than the previous one.
number = input('please enter a number:')
val = int(number)
if val > 0:
integer = raw_input('please pick a second integer:')
if raw_input < val:
print 'please pick an integer greater than the previos input'
if raw_input > val:
print
if val < 0:
print 'please pick a positive integer greater than zero'
You could ask for the inputs before the while loop check and then ask again, but I like the cleaner appearance of only having the input prompt appear once in the code, so we can set up some conditions that will trigger the loop and the prompt.
We can initialize num1 = -1 and then our while loop condition will be triggered and repeat until we receive and int larger is larger than 0.
Then we can do the same with num2 by initializing it as num1 - 1, this will trigger our while loop that will continue to prompt until num2 is greater than num1.
Finally we can print a list of the range from num1 to num2 + 1 since the end is not inclusive we should extend the range by 1
num1 = -1
while num1 <= 0:
num1 = int(input('Enter a number greater than 0: '))
num2 = num1 - 1
while num2 <= num1:
num2 = int(input('Enter a number greater than {}: '.format(num1)))
print(list(range(num1, num2+1)))
Enter a number greater than 0: 1
Enter a number greater than 1: 5
[1, 2, 3, 4, 5]