This question already has answers here:
Why is "None" printed after my function's output?
(7 answers)
Closed 3 years ago.
I tried to make a program to determine whether a number is odd or not, I get the following output
def is_odd(n):
if n % 2 != 0:
print(True)
else:
print(False)
if I put
print(is_odd(1))
I get:
True
None
Whatever number I choose, I always get
None
at the end.
You have to return something to remove this None . You can also modify the code by returning the True or false and print it in the main
Related
This question already has answers here:
How to test multiple variables for equality against a single value?
(31 answers)
Closed 12 months ago.
I'm a beginner in Python and I'm trying to write an IF statement with multiple OR conditions in the expression. My problem is that the expression is only "True" for the first OR condition.
def rna_length(mrna):
if (mrna[-3:]) == ("UGA" or "UAA" or "UAG"):
return print("TRUE!")
else:
return print("False")
rna_length('AUGAGGCACCUUCUGCUCCUUAA')
I was expecting True after running the code but the code is printing False.
Kindly help me understand my mistake.
Here is how to do:
if mrna[-3:] in ("UGA", "UAA", "UAG"):
("UGA" or "UAA" or "UAG") will return "UGA" because the operator or return the first non-False value. Since non-empty strings arn't False the first value is returned.
This question already has answers here:
How can I read inputs as numbers?
(10 answers)
Closed 2 years ago.
I'm working through some practice questions in the book "Automate the Boring Stuff," and I'm struggling to understand why the code below only produces 'Greetings!' as output.
print('What does spam equal? Hint: Try a number between 1 and 3')
spam = input()
if spam == 1:
print('Hello')
elif spam == 2:
print('Howdy')
else:
print('Greetings!')
It does because what you have given as input is stored as string in spam . And when you are using if else statements then it is comparing with integers, but your actual value is a string, therefore it always turns out to be in the final else statement printing Greetings!
So use spam=int(input()) instead.
This is because the input() returns a string, which is never equal to an integer.
Try
spam = int(input())
instead. This will of course throw a ValueError if the input is not an integer.
This question already has answers here:
How are strings compared?
(7 answers)
Closed 2 years ago.
Why the result of “bag” > “apple” is True in Python?
I tried this code below i don't know why it show this result and how? Please some one explain it.
print("bag" > "apple")
True
I believe this is True because Python compares the first letter of each word. And b is greater than a in Python.
This question already has answers here:
Why is "None" printed after my function's output?
(7 answers)
Closed 5 years ago.
I'm trying to create a function that uses a while loop to count up from one to a number given by a user. The code executes as I intend it to but returns None at the end. How do I get rid of the None? Here's the code.
def printFunction(n):
i = 1
while i <= n:
print(i)
i+=1
print (printFunction(int(input())))
You can use this code to prevent none, tough its just the last line changed
def printFunction(n):
i = 1
while i <= n:
print(i)
i+=1
printFunction(int(input()))
In the last line you were using
print(printFunction(int(input()))) which was getting you None after printing the results.
Instead just use printFunction(int(input())). This will not print None. You can also use a message to ask user like printFunction(int(input("Enter a number"))). Since there is noting getting returned you no need to use print.
This question already has answers here:
How to test multiple variables for equality against a single value?
(31 answers)
Closed 7 years ago.
I have an array of two int's, and I want to check if either is None, so I have this:
print hourArray
if hourArray[0] or hourArray[1] is None:
print "FAILED???"
else:
print "array is full"
And even though the print hourArray shows this right before the if statement
[2040, 2640]
It prints FAILED??? even though neither of the elements in the array is None?
Why is this happening?
The issue is that you are checking if (hourArray[0]) or (hourArray[1] is None) , all non-zero integer values are always true.
You should do -
if hourArray[0] is None or hourArray[1] is None:
Example of non-zero integer values being true -
>>> if 1:
... print('Hello')
...
Hello