This question already has answers here:
Python: False vs 0
(3 answers)
Closed 6 years ago.
I'm trying to configure the following if else :
Basically 0 is equals to False in Python...
I would like to understand why this happens and how I can workaround in this case :
x=0
if x==False:
print "x == False" #always prints this line
elif x == 0:
print "x == 0"
Appreciate the help:
EDIT:
already tried x = int(0)
You should use x is False to check whether x is exactly False.
I doubt you really need it though.
if x is False:
print "x is False" #always prints this line
elif x is 0:
print "x is 0"
Because 0 == False but 0 is not False.
Related
This question already has answers here:
Is False == 0 and True == 1 an implementation detail or is it guaranteed by the language?
(3 answers)
Why is bool a subclass of int?
(3 answers)
Closed 13 days ago.
a = True
match a:
case False:
print("It is false")
case 2:
print('It is 2 true.')
case 1:
print('It is 1 true.')
>>> It is 1 true.
Why does the program execute the last case statement?
This question already has answers here:
What is the purpose of the return statement? How is it different from printing?
(15 answers)
Closed 6 months ago.
just a total newbie here. I am learning Python and was trying to call the function as below, yet I cannot see any output.
nums = [0,1,2,7,14]
def has_lucky_num(nums):
#return numbers that can be divisible by 7
for num in nums:
if num % 7 == 0:
return True
return False
has_lucky_num(nums)
Can someone tell me why it did not work? Thanks in advance.
Your program isn't printing anything because it doesn't have a print() command anywhere. Try the following line : print(has_lucky_nums(nums)), and it should print your True or False result! So you would have the following code :
nums = [0,1,2,7,14]
def has_lucky_num(nums):
#return numbers that can be divisible by 7
for num in nums:
if num % 7 == 0:
return True
return False
print(has_lucky_num(nums))
Your function has a problem:
You must put the return False after loop because if you don't it return immediately after checking first item
So function code must be this:
nums = [0,1,2,7,14]
def has_lucky_num(nums):
#return numbers that can be divisible by 7
for num in nums:
if num % 7 == 0:
return True
return False
If you want to see result you must print the result returns by the function has_lucky_num()
So all you have to do is to add a print like this:
Instead Of:
has_lucky_num(nums)
Replace With:
print(has_lucky_num(nums))
Output:
True
Return True and Return False won't actually display anything. You need to do something with those values, for instance:
if has_lucky_num(nums) == True:
print(f'{nums} has a lucky number!')
else:
print(f'{nums} has a lucky number!')
```
This question already has answers here:
Why does "a == x or y or z" always evaluate to True? How can I compare "a" to all of those?
(8 answers)
Closed 1 year ago.
I have tried to write a recursive version of the Palindrome function. But it is giving me True in all the cases. why?
def isPalindrome(inputString):
if len(inputString)==0 or 1:
return True
elif inputString[0]==inputString[-1]:
return isPalindrome(inputString[1:-1])
else:
return False
Why False is not working for non Palindrome Text.
Short circuit evaluation.
def is_palindrome(input_string):
if len(input_string) == 0 or len(input_string) == 1:
return True
elif input_string[0] == input_string[-1]:
return is_palindrome(input_string[1:-1])
else:
return False
print(is_palindrome('your_string')) # False
This question already has answers here:
How can I read inputs as numbers?
(10 answers)
Closed 1 year ago.
I don't understand what's wrong with my code here. Can someone help me pls? I've been trying to solve it all morning.
question = input("Choose from 0 to 1 : ")
mylist = ["Mark", "Jenny"]
if question == 0:
print(mylist[0], "is your new friend")
elif question == 1:
print(mylist[1], "is your new friend")
else:
print("I said choose from 0 to 1")
The problem is in the data types:
input() returns a string but in your, if statement you're comparing a string "0" to an integer 0. Because of that else is always executed.
Concert the input() into int() like shown below:
question = int(input("Choose from 0 to 1 : "))
mylist = ["Mark", "Jenny"]
if question == 0:
print(mylist[0], "is your new friend")
elif question == 1:
print(mylist[1], "is your new friend")
else:
print("I said choose from 0 to 1")
This question already has answers here:
Why does "a == x or y or z" always evaluate to True? How can I compare "a" to all of those?
(8 answers)
Closed 6 years ago.
So the problem I'm having is with this part of a simple script for a Chat Bot about Programming. But when I test this part of the script and type in 'B' as my input, it says ('Panda: I'm glad you share my enthusiasm.') which is what is mean't to happen if I input A! Can someone please point out to me what I'm doing wrong?
invalidAnswer2 = true
while invalidAnswer2 == true :
answer2 = input (user)
if answer2 == ('a') or ('A') :
print ('Panda: I'm glad you share my enthusiasm.')
invalidAnswer2 = false
elif answer1 == ('b') or ('B') :
print ('Panda: I wish. I am actually British, but I dream of going to America!')
print ('Panda: *Cough* Um anyway that was rather unproffesional of me.')
invalidAnswer2 = false
else :
invalidAnswer()
invalidAnswer2 = true
Thank You for helping me fix it. It was a simple typo after all that XD
The problem is your that your first if statement is always True:
if answer1 == "a" or "A"
Will always yield true, because it's really asking:
if (answer1 == "a") or ("A")
And "A" will always return True.
What you meant is probably:
if answer1 == "a" or answer1 == "A"
or the better option:
if answer1.lower() == "a"