If/else always returns bad [duplicate] - python

This question already has answers here:
How to test multiple variables for equality against a single value?
(31 answers)
Why does "a == x or y or z" always evaluate to True? How can I compare "a" to all of those?
(8 answers)
Closed 11 months ago.
I just started learning python, and now I'm trying to make some very newbie programming on my own, with my own ideas. However, I just cannot figure this out:
print("It is 8:11 AM. You have no job to go to. Do you want to spend some hours looking and applying for jobs? (Y/N)" )
answer = input()
resume = 0
if answer == "N" or "n" and not "Y" or "y":
print("Fak")
else:
if answer == "Y" or "y":
if resume == 0:
print("You still haven't done your resume. You cannot apply to a job without one.")
else:
print("You have found 2 jobs, but your experience level was too low for them.")
print("It is 11:24 AM. What would you like to do?")
No matter how I tried, with my knowledge, the "Fak" always gets printed. I just can't figure it out, it seems right to me.
Where did I fak up?
Sorry if the formatting or anything is wrong, this is my first time using Stackoverflow.

Related

How to restart a code included into a function? [duplicate]

This question already has answers here:
How to test multiple variables for equality against a single value?
(31 answers)
Closed 1 year ago.
I would like to restart my python code from the beginning after a given "yes" input from the user, but I can't understand what I'm doing wrong here:
if input("Other questions? ") == 'yes' or 'yeah':
main()
else:
pass
my code is included within the function main().
Thanks for the help!!
You would probably do it with a while loop around the whole thing you want repeated, and as Loic RW said in the comments, just break out of the while loop:
while True:
# do whatever you want
# at the end, you ask your question:
if input("Other questions? ") in ['yes','yeah']:
# this will loop around again
pass
else:
# this will break out of the while loop
break

Why break function is not working and program says it's out of loop? [duplicate]

This question already has answers here:
How do I terminate a script?
(14 answers)
Closed 3 years ago.
My break function is not working even though it is in the loop. Please, someone, explain in detail, because I'm new to Python.
q1 = input('want a question?: ')
if q1 == 'yes':
print("let's get started!, press enter")
a1 = input()
else:
print('why did you even start me ?!')
break
Here I expect break to stop the program from going any further.
break doesn't stop your program from going any further. It breaks out of a loop.
You may want sys.exit, e.g.
import sys
sys.exit()

What is wrong with these if and else statements? [duplicate]

This question already has answers here:
How to test multiple variables for equality against a single value?
(31 answers)
Closed 5 years ago.
i am new with python and i am trying to make a survey but when i write this code things don't go well
this is the first part of my very long survey:
#a program to test your adhd
yes=1
YES=1
Yes=1
no=0
NO=0
No=0
print("please honestly answer the following questions","\n"
"with \"yes\" or \"no\" ")
a=input("1. do you have difficulty getting organized ?")#q1
if a==yes or YES or Yes or no or NO or No:
b=input("2. When given a task, you usually procrastinate rather than doing it right away")#q2
else:
print("wrong answer")
a=input("1. do you have difficulty getting organized ?")#q1
the idea of this is when the user write one of the true answers the program move to the next question.
and if he wrote other things the program print wrong answer and repeat the question.
but when tested with python shell and c.m.d it never consider the (else statement)
note that: i don't know many things in python (besides if and else statements) yet
as i am at the very beginning on learning steps.
Notice that a is a string, and you'll have to test each condition separately (don't forget the quotes!), like this:
if a == 'yes' or a == 'YES' or a == 'Yes' or a == 'no' or a == 'NO' or a == 'No':
Or a simpler alternative:
if a.lower() in ('yes', 'no'):

If statement with or not working as expected [duplicate]

This question already has answers here:
How to test multiple variables for equality against a single value?
(31 answers)
Closed 7 years ago.
My program first asks the user what is wrong with their mobile phone and when the user writes their answer, the program detects keywords and outputs a solution. I used "or" statements but when I input "My phone dropped in water and my speakers don't work" it should output "you need new speakers. if waters been in contact you also may need a new motherboard if it doesn't come on" but instead it outputs "you will need your screen replacing"
Can anybody tell me what I'm doing wrong and tell me how I can overcome this problem in the future.
import time #This adds delays between questions
name = input ("what is your name?")
problem1 = input("hello there,what is wrong with your device?")
if "cracked" or "screen broke" in problem1:
print("you will need your screen replacing")
elif "water" or "speakers" in problem:
print("you need new speakers. if waters been in contact you also may need a new motherboard if it doesnt come on")
I think you mis-understand or. In programming, or is slightly different from in English. x or y in z means "is x True or is y in z?". If the first argument (x) is non-empty, that means that it evaluates to True. No check is made for anything's presence in z. If x is empty, it evaluates to False and there is a check for y's presence in z, but not x's presence. You need to create a new test for each one. Also, you used problem instead of problem1 in your second elif:
if "cracked" in problem1 or "screen broke" in problem1:
...
elif "water" in problem1 or...
...

I can't seem to get user input to work in my if, elif, else loop in Python [duplicate]

This question already has answers here:
How to test multiple variables for equality against a single value?
(31 answers)
Closed 8 years ago.
So I am working on a program that essentially asks a person if they want to hear a joke; if they say yes, then it will proceed to tell a joke; if they say no, or stop/quit the program will stop; and if they don't say yes or no, it will give them an error message. However, the program seems to just want to proceed on with the joke no matter what they input. I will produce an example of the code.
import sys
print("Want to hear a joke?")
answer = input()
if answer == "Yes" or "yes" or "y":
print("Here is a joke")
elif answer == "No" or "no" or "n":
sys.exit("Bye!")
else:
print("Please input a different response.")
This is NOT how you do a conditional
if answer == "Yes" or "yes" or "y": # this will always evaluate to true
do this instead
if answer in ["Yes" ,"yes" , "y"]:
print "Here is a joke"

Categories

Resources