Reputation: 83
I've been having trouble printing a list in python because whenever I give the command to show the list it does not show that list. Heres the code for the whole function reference the list is in:
#FUNCTIONS
def help():
print("list of commands\n"
+ "help = Display of commands\n"
+ "list = list of all the Latin I vocabulary\n"
+ "Quit = exits the main program to the exit credits and then exits the app\n")
def userInstructions(userInput):
if (userInput == "help" or "Help"):
help()
elif(userInput == "list" or "List"):
list()
return input("\nEnter your responce: ")
def list():
a = ["salve" , "vale" , "et" , "est" , "in" , "sunt" , "non" , "insula" , "sed" , "oppidum"
, "quoque" , "canis" , "coquus" , "filia" , "filius" , "hortus" , "mater" , "pater" , "servus" , "via" , "amicus" , "ancilla" , "cena" , "cibus"
, "intro" , "saluto" , "porto" , "video" , "dominus" , "laetus" , "mercator" , "audio" , "dico" , "unus" , "duo" , "tres" , "quattuor" , "quinque"
, "sex" , "septem" , "octo" , "novem" , "decem" , "ad" , "ecce" , "magnus" , "parvus" , "ambulo" , "iratus" , "quis" , "quid" , "cur" , "ubi" ,
"sum" , "es" , "eheu" , "pecunia" , "ego" , "tu" , "habeo" , "respondeo" , "venio" , "rideo" , "quod" , "ex" , "voco" , "clamo" , "specto" , "taberna"
, "laboro" , "clamor" , "femina" , "vir", "puer" , "puella" , "multus" , "urbs" , "agricola" , "curro" , "hodie" , "iuvenis" , "meus" , "senex" , "sto" ,
"optimus" , "volo" , "fortis" , "emo" , "pulso" , "bonus" , "malus" , "festino" , "per" , "pugno" , "scribo" , "tuus" , "erat" , "paro" , "cum" , "facio" ,
"heri" , "ingens" , "nihil" , "omnis" , "vendo" , "navis" , "prope" , "rogo" , "terreo" , "inquit" , "tamen" , "eum" , "eam" , "duco" , "saepe" , "interficio" ,
"habito" , "silva" , "statim" , "totus" , "pessimus"]
print("List:")
print('\n'.join(map(str, a)))
The picture below shows the result when I command the code to print the list which instead of printing the list it prints the help panel instead:
What's the problem in my code and How do I fix it?
Upvotes: 0
Views: 147
Reputation: 5700
The culprit is:
if (userInput == "help" or "Help"):
You need:
if userInput in ('help', 'Help'):
or:
if userInput == 'help' or userInput == 'Help':
The precedence of '==' is greater than 'or', so your 'if' is being treated as:
if (userInput == 'help') or ('Help'):
Because the 'Help' is logically equivalent to 'True', you never get past the first if
check.
When you want to be case-insensitive, just convert to all upper or lower before checking. So you could also say:
if userInput.lower() == 'help':
There are many different ways to do this. Some people think that certain ways are much better than other ways. But the trick is to get it to work.
Also, as a side note, you can just say '\n'.join(a)
without the map
and str
, since it looks like all the entries in your list are already strings. If you might have other things in there, too, then the map
and str
are helpful.
Upvotes: 2
Reputation: 2287
userInput == "help" or "Help"
is interpreted by Python as (userInput == "help") or "Help"
, which will always be true. Instead try:
userInput == "help" or userInput == "Help"
or
userInput in ["help","Help"]
or
userInput.lower() == "help"
(And likewise for userInput == "list" or "List"
).
Also I don't recommend naming your function list()
, it conflicts with a built-in python function.
Upvotes: 3