Home > Enterprise >  PYTHON: Whenever I input something, I want my code to print multiple possible possible values
PYTHON: Whenever I input something, I want my code to print multiple possible possible values

Time:08-08

The title explains it all, but basically I want my python code to output multiple possible input values, here's my code which might make it easier

word = input("Input a word/sentence").upper().title()


if word == "Yes":
     print("Dar")
elif word == "No":
     print("Jor")
elif word == "Maybe":
     print("Jard")
elif word == "Definitely":
     print("forswer")
elif word == "Absolutely":
     print("Arsry")
elif word == "Perhaps":
     print("Åsët")
elif word == "Of course":
     print("Aresøt")

So how can I make it so whenever I input "Perhaps , Definitely" that it shows both? Whenever I do that it obviously doesn't print anything.

CodePudding user response:

So you want it to output "Åsët, forswer" for the example?

A possible way is to first break the input string into a list of word by splitting on "," and trimming then leading and trailing spaces. Then for each word in the list, translate it.

The translation part is almost identical to the code you have right now, except instead of printing it, you can save it into a list. Finally, when you have all the translations, oncatenate the list of translated words with ", ".

CodePudding user response:

You will need to search in the string, right now you are just comparing the whole string. So your check would look something like this:

if "Yes" in word:
     print("Dar")

There are other optimal solutions for this, but this should get you started.

CodePudding user response:

You would need to create a dictionary and then replace:

word_dictionary = {'Yes':'Das','No':'Jor','Maybe':'Jard','Definitely':'forswer','Absolutely':'Arsry','Perhaps':'Åsët','Of course':'Aresøt'}

And then use the following to do the replacements:

','.join([word_dictionary.get(x.strip()) for x in text.split(',') if word_dictionary.get(x.strip())])

For example:

text = "Yes, Of course, but, No"

Returns:

'Das,Aresøt,Jor'
  • Related