0
If else for strings??
how would I write an input for a string, for example if I wanted to ask what is your favorite color and depending on their favorite color they get a different response. I tried writing codes for this but kept getting errors.
2 Answers
0
You can use a simple if/else if/else statement as you suggest in your title. The thing is, to achieve interactivity, it will depend on the number of cases you deal with in your statement, e.g. a simple input and two conditions on the colors "blue" and "yellow":
print ("What is your favorite color?")
color=input()
if (color.lower()=="blue"):
print("Cool, me too")
elif (color.lower()=="yellow"):
print("Arf, hate it")
else:
print("I don't know the color", color)
Two things :
- Always have a default case for everything that will not fall under your if/else if (who knows what the user might input)
- the use of .lower() method on the imput , so that the comparison sill consider the input all in lower case (so that the user can put Blue, BLUE, bLuE, or whatever, the comparison will always consider it as blue)
EDIT : if something in particular seems to not be working, or if you have a special implementation in mind, expand and share your code so we can work on it