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...
...
Related
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.
I am trying to a make a simple game. I will supply of a dictionary of states and capitals and a list of states. Using loops and conditionals, I will ask the user if they want to learn some capitals and supply the list of states. The state gets removed from the list and the user should be prompted again if they want to play, repeatedly until the list is empty. With my code right now the loop piece works, if keeps asking if they want to play and as long as the user keeps saying yes my code works running till the list is empty and the loop. But when I try to add a layer for if the player says no and break the loop its not doing anything. Thanks in advance for help!!
states3 = ["NH", "MA", "MS", "SC", "HI"]
print("Let's test my geography skills!")
def state3(states3):
state_caps = {"NH": "Concord", "HI": "Honolulu", "SC":"Columbia", "MS": "Jackson", "MA":"Boston"}
play = input("Would you like to learn some capitals:")
while play == "Yes" or "yes":
if play == "Yes" or "yes":
print ("The states I know the captials of are:", states3)
yourstate = input("What state do you want to know the capital of: ")
print("The capital of", yourstate, "is", state_caps.get(yourstate, "That is not a vaild choice"), "!")
states3.remove(yourstate)
play = input("Would you like to learn some capitals:")
if len(states3) == 0:
print ("That's it! That's the end of geography skills")
break
state3(states3)
while play == "Yes" or play == "yes":
if play == "Yes" or play == "yes":
print ("The states I know the captials of..")
.....
....
elif play == "no" or "No":
break
Checking for yes/no is problematic. For example, "YES" is not covered in the above code, yet seems a reasonable response. To distinguish between the two choices, do we need to look at the entire word or only the first letter? (string "slicing")
Such reduces the set of applicable responses to the set: y, Y, n, N. Now, if we apply the string.lower() or string.upper() method*, we are down to two choices.
However, consider this even more closely. Is there really only one user-response that interests us? That the game should continue. Thus, any response other than 'yes' could be considered to mean stop looping!
Another question: once the while-condition has been satisfied and the loop starts to run, is it possible for the first if-condition to be anything other than True? Could the latter be removed then?
Instead of merely adding a "layer", let's review the entire game. How's this for a spec[ification]: repeatedly invite the user to choose a state, and then present its capital.
How do we define "repeatedly", or more precisely, how do we end the looping? There are two answers to that question: either all the states have been covered, and/or the user loses interest (the new "layer"). What happens if we use (both of) these to control the while-loop? (NB reversing the logical-statement) Loop if there are states to review and the user consents. Thus, can the last if-condition move 'up' into the while-condition...
Because you are evidently (enjoying) teaching yourself Python, I have left the writing of actual code to your learning experience, but herewith a few items which may increase your satisfaction:-
in the same way that the 'heading print()' is outside the loop, consider where the farewell should be located...
A list with contents is considered True, whereas an empty/emptied list is considered False. Very handy! Thus, you can ask if states3 (ie without the len()).
also consider the upper()/lower() 'trick' when accepting the state's abbreviation-input.
I am VERY new to coding in general so this may come across as a stupid question.
Right now I am experimenting with conditional statements and trying to code a little decision based two-way path in the style of a text game, where the user is given two options and each option has a different outcome. Right now, the "if True" string is ALWAYS executed in the console, regardless of what the user types in. Why is this happening and how can I rewrite it to make the command happen correctly? Also, I thought I followed the instructions for pasting code here but it doesn't look quite right.
input ("Type 'left' if you want to go left and 'right' if you want to go right"
right = True
left = False
right = "right"
left = "left"
if True:
print("You have been eaten by a monster. Game Over!")
else:
print("Congratulations you have made it to the castle!")
else:
print("Error")```
There's no such thing as a stupid question!
In any programming language, when you come across an if-statement, you should read it as "if this statement evaluates to true, do this." So, in your example, writing
if True:
# do something
will always run that code, because True always evaluates to True!
It sounds like what you want to do is compare the player's input to some set string (in this case, "right" or "left"). The first thing you should know is that the input() function returns, as a string, whatever the user input before hitting enter. So, in this case, you might want to do something like
direction = input("Would you like to go left or right?")
Now, once the user inputs something and hits enter, whatever they entered will be saved in the variable direction. Now, we can compare the entered string to whatever we want. In this example, we would want something like
if direction == "left":
# do left stuff
elif direction == "right":
# do right stuff
else:
# they didn't enter a valid string!
I hope this helps. If you're just starting out on your programming journey, I would recommend looking up some Python tutorials online which are specifically made for beginners, and try finding one that suits your learning style. Best of luck!
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'):
This question already has answers here:
Asking the user for input until they give a valid response
(22 answers)
Closed 6 years ago.
Question1 = input("We will start off simple, what is your name?")
if len(Question1) > 0 and Question1.isalpha(): #checks if the user input contains characters and is in the alphabet, not numbers.
Question2 = input("Ah! Lovely name, %s. Not surprised you get all the women, or is it men?" % Question1)
if Question2.lower() in m: #checks if the user input is in the variable m
print ("So, your name is %s and you enjoy the pleasure of %s! I bet you didnt see that coming." % (Question1, Question2))
elif Question2.lower() in w: # checks if the user input is in the variable w
print ("So, your name is %s and you enjoy the pleasure of %s! I bet you didnt see that coming." % (Question1, Question2))
else: #if neither of the statements are true (incorrect answer)
print ("Come on! You're helpless. I asked you a simple question with 2 very destinctive answers. Restart!")
else:
print ("Come on, enter your accurate information before proceeding! Restart me!") #if the first question is entered wrong (not a name)
Question3 = input("Now I know your name and what gender attracts you. One more question and I will know everything about you... Shall we continue?")
In order to get the right answer, I will first tell you what happens when I run it:
There are several scenarios but I will walk you through 2. When I run the program I am first asked what my name is, I can enter anything here that is alphabetic, then it asks me if I like men or woman, weird I know but it's a project I am working on, if I were to say 'men' or 'women' the program runs perfectly, but if I were to enter say... 'dogs' it would follow the else statement print ("Come on! You're helpless. I asked you a simple question with 2 very destinctive answers. Restart!") but then it continues the code and goes to the last line shown above "Now I know your name and what gender attract you...... blah blah". What I am trying to do is have it restart the script if you were to enter anything other than 'men' or 'women'.
I was told that a while statement would work, but I would need an explanation as to why and how... I'm new to Python in a sense.
That's what loops are for.
while True:
# your input and all that here
if answer == "man" or answer == "woman":
# do what you want if the answer is correct
break # this is important, as it breaks the infinite `while True` loop
else:
# do whatever you want to do here :)
continue
If you want the script to repeat indefinitely, you can simply wrap everything in a while loop like so:
while True:
# Do yo your logic here.
# Break out when a condition is met.
If you want to be more savvy with it you can use a method and have the method call itself over and over until it's done.
def ask_questions():
# Do your logic here
if INPUT_NOT_VALID:
ask_questions()
else:
# Continue on.
Also, search on this site for you text because I've seen about five or six questions all involving this programming scenario which means it has to be a common problem from a programming class (one I'm assuming you are in). If that's the case discuss with classmates and try to stimulate each other to find the solution instead of just posting online for an answer.