This question already has answers here:
How to test multiple variables for equality against a single value?
(31 answers)
Closed 2 years ago.
can someone explain me why when I use this if statement:
if template_decider != ("B" or "P"):
something_went_wrong()
else:
print("Hello")
If user inputs B the statement returns "Hello" but if user press P it resturns function something_went_wrong?
I'm really confused why it not always returns "Hello"
Thank you.
You wrote incorrect statement. But it's possible to do what you want with this code:
if template_decider not in ("B", "P"):
something_went_wrong()
else:
print("Hello")
Related
This question already has answers here:
It is more efficient to use if-return-return or if-else-return?
(9 answers)
Closed 6 months ago.
I've always had the habit to put else statements but I'm wondering if there's any reason not to, if it works.
Here's an example:
def multiply_by_two(number, display_original_number=False):
result = number*2
if display_original_number is True:
return f'Multiplying {number}: {result}'
return f'{result}'
print(multiply_by_two(5, True))
print(multiply_by_two(5))
Output:
Multiplying 5: 10
10
If I put else: return f'{result}', it's bascially the same thing, right? Is there any reason I would want to put an else statement in such cases?
Within this use case there is no difference functionally. Personal preference would dictate this decision if you find it easier to read back your code using else: .
This question already has answers here:
Why is the purpose of the "else" clause following a "for" or "while" loop? [duplicate]
(2 answers)
Closed 2 years ago.
I am new to python programming. I am a little confused in the below code, how else statements work without corresponding if statement. Could anyone please explain to me the below code. Program is Prime number between two intervel.
start=int(input("Enter Number: "))
stop=int(input("Enter Second Number: "))
for i in range(start,stop):
if i>1:
for j in range(2,i//2+1):
if(i%j==0):
break
else:
print(i)
In Python, loops can have an else statement. However, think more of a try-except statement than a if clause.
An example:
for item in container:
if search_something(item):
# Found it!
break
else:
# Didn't find anything..
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)
Comparing a string to multiple items in Python [duplicate]
(3 answers)
Closed 2 years ago.
Note: I'm not a fluent english speaker, so you may see some grammar mistakes here. I'm kinda new to programming and i know i can fix it by putting another else/elif statement, i just trying to get the short possible lines to avoid spamming elif like yandev.
so the thing is, at the #functions i already put if below the gae variable's input, which says that if the input = y or Y then it runs the line below it, which is print("good. go to www.minecraft.net"). Now my problem is, when i try to input other letter than Y, it still runs the same line whilst there's already an else line underneath that i think should exclude other words than Y or y. For example, i already input the word "n" at the output console(?) but the output says "good. go to www.minecraft.net" instead the lines at the else statement. is there any possible solution without adding much more elif/else statement?
Click this link for the image to understand what am i talking about
The line if gae == "Y" or "y" should be if gae == "Y" or gae == "y" otherwise it will always be true and the code will always print "good. go to www.minecraft.net".
This question already has answers here:
String comparison in Python: is vs. == [duplicate]
(4 answers)
Closed 6 years ago.
I wrote this very simple code, it doesn't work and I can't understand why. I answer 'good' but if part doesn't work!
def howdy ():
p = input("how's it going?")
if p is "good":
print("Cheers")
howdy()
You have to use:
If p == 'good':
The == compares the value of the two. The Is keyword checks for identity by comparing the memory address.
Hope this explains it enough.
Hannes
have you tried
if p == "good":
the double equal means the same as.
Also, if you are looking for a helpful free beginner course, try codecademy. They helped me loads learn python
This question already has answers here:
String comparison in Python: is vs. == [duplicate]
(4 answers)
Closed 6 years ago.
I am new to programming and using Python 3.5. When ever I run the following code it skips my if and elif statement and goes straight to else after entering input values that should run in the if or the elif lines of code (ie input entered is chris or sam). I don't understand why that is considering that all values are strings.
Here is my code:
name = input('What is your name?\n')
name = name.lower()
if name is 'chris':
print('Hi Chris!')
elif name is 'sam':
print('Hi Sam!')
else:
print('Who are you?')
Thanks in advance :)
You should be using == not is. Is tests whether one object is the same type as another (if value is None for e.g).