I got something wrong with the else statement [duplicate] - python

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

Related

Is putting an "else" in this case relevant or not, and why? [duplicate]

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: .

Adding error check gets script stuck in infinate loop (python) [duplicate]

This question already has answers here:
How can I read inputs as numbers?
(10 answers)
What's the canonical way to check for type in Python?
(15 answers)
Closed 2 years ago.
I'm working on a script to take user input and write it into an Excel file
currently the script works but when trying to add in logic to reject non-integer answers, one of two things happen:
it gets stuck in an infinite loop after properly rejecting the first answer where it keeps asking for a new answer and never accepts it
it properly rejects the first answers, but then accepts the second no matter what it is
this is the code being used where it gets stuck in an infinite loop (I have tried a few other variations that do the same thing but this is the most recent)
true = True
x = input('What is the first marked distance?')
while true == True:
if x == int:
cl.value = x
break
else:
x = input('Please enter a whole number')
any advice on why this is looping and not working would be greatly appreciated
Use float(x)%1 == 0 instead of x == int

In Python input returned does not get detected by my if or elif statements skips straight to else statement, why? [duplicate]

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

Python - attempting to make a morse code LED [duplicate]

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 attempted to write the code such that it will tell you to input your string. at which point you type a string for example lets say you type "purple" then press enter. the code will run and get the length, then instead of getting the first letter variable[0] as P and then lighting the LED in the code for P, it constantly interprets all letters as A. i know its going to be something small but ive only been learning python for a few days so try to be nice. i have previous experience with VB so i have most likely used the wrong syntax somewhere but i cant figure it out. PLEASE HELP.
i cant copy the code into this without rewriting it so im going to use screenshot links.
this is where i think the problem must be
http://puu.sh/lUf9q/3ad50c4faf.png
and then this is how i've set the morse code patterns
http://puu.sh/lUfgo/f6f3f2cb32.png
the reason there is 2 = and 1 == is because i was tinkering with them to see if they were the problem which they aren't
Thanks in advance
EDIT:
apparently i have to edit it to say why its different to said thread. The way its different is: its not but I didn't realise that the problem in the thread was the problem i was having.
It is interpreting all letters as "a" because your if statement says
if curr = "A" or "a":
In fact == and = are different. One does assignment, and the other checks for equality. This if statement always executes because there is always one side of the or that is true.
Try this instead
if curr.lower() == "a":

Control statement confusion in Python [duplicate]

This question already has answers here:
What does placing \ at the end of a line do in python?
(3 answers)
Closed 8 years ago.
x is an array, y is a dict which is a member of the method z. What does the '\' mean? What does this code do?
for x, y in \
self.z({'yup': [10]}):
for typex in x:
//if statement
EDIT: Thank you for the help, I'm still unsure of what is being iterated over here, the part that is confusing me is the for x,y in as opposed to me usually seeing a statement for only a single variable
The \ is a line continuation, allowing the statement to continue to the next line without raising an indentation error. Aside from that this is just a vanilla for loop.
The \ is simply a line breaker.
The code is not complete, it's just the first part of the for loop. What x in y are iterating over in the loop will depend on the method/function z.

Categories

Resources