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

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'):

Related

If/else always returns bad [duplicate]

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.

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

End an if statement without moving to next lines [duplicate]

This question already has answers here:
How to use sys.exit() in Python
(6 answers)
Closed 4 years ago.
So I'm trying to make a little fun code to test my skills, and I'm trying to get the first if statement to continue to the next if statement, that is if they answer yes. If they answer anything other than yes, the code ends. How do I change it so the code actually ends if they answer incorrectly?
print('Would you like to begin?')
answer = input()
if answer == 'yes':
print('What is your name?')
#continue to the next portion when if statement is true
else:
print('ok')
#break the code when else statement is true
name = input()
if name == 'Test':
print('Congratulations! You won!')
else:
print('Good try. The answer was Test.')
Replace #break the code when else statement is true with import sys; sys.exit(0).
If they answer 'yes', it will simply continue to name = input().

Function returns "None" (not using any fancy functions in the code) [duplicate]

This question already has an answer here:
print statement inside of input returns with a "none"
(1 answer)
Closed 6 years ago.
I am writing a game using loops and I haven't defined any functions in my code, but only using basic ones like print(). My code itself does exactly what it's supposed to do, but it throws in a None where the user is supposed to give input to the code.
while loca == 22:
if (lever=="back") and (dial=="red"):
print("congratlations! You have won the game!")
play=input(print("Would you like to play again? Type 'Y' for yes and 'N' for no."))
while play in ["yes","y","Y","Yes"]:
loca = 1
play="reset"
while play in ["no","n","N","No"]:
print("Thanks for playing!")
quit()
while (not play == "reset"):
while (not play == ["no","n","N","No"]) and (not play == ["yes","y","Y","Yes"]):
play=input(print("Sorry, I didn't understand. Please enter 'Y' for yes, or 'N' for no."))
else:
print("Hmm.. You don't quite have the right combination yet!")
I know the quit() is a function, but I also tried the code with it removed and it still returned a None. I have never had this problem with other programs I wrote that used the same function I am using in this code.
For this assignment, we are supposed to implement the quit function into the code, or some variation of it but we haven't learned about a return for functions (this code isn't supposed to have any author defined functions) and it seems that the only answer I can find online is to do with a return. I thought I would try my luck here and maybe it is just something small that I am missing.
The print function returns None, which you're then passing to input, remove the calls to print, since input already takes care of printing the string passed.
Instead of:
play=input(print("Would you like to play again? Type 'Y' for yes and 'N' for no."))
Use:
play=input("Would you like to play again? Type 'Y' for yes and 'N' for no.")
The line where you create the variable play. You misuse the input function the prompt you write in the function already is displayed for the user. However by putting in print() you return none.
That line should be:
play = input("Would you like to play again? Type 'Y' for yes and 'N' for no.")
P.S. you should try and conform to the PEP 8 - python style guide

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