Home > Blockchain >  Get the second value in a tuple by using input from the user to search for the first one
Get the second value in a tuple by using input from the user to search for the first one

Time:11-15

Each possible answer has a certain amount of points associated with it. If the player guesses an answer that exists in the possible answers, they will be rewarded with the corresponding amount of points.

If I have a tuple named "answer"

possible_answer = [("LEMON/APPLE", 25), ("CHEESE/ICE", 27), ("TOOTHPASTE", 19)]

I need a program to check the answer against the possible_answer and print the appropriate points.

Example:

# Test case 1

input: LemoN
needed output: 25 


# Test case 2

input: TooTHPASte 
needed output: 19 

CodePudding user response:

I suggest building a dictionary that maps each answer (normalized to lowercase) to its score.

>>> answer = [("LEMON/APPLE", 25), ("CHEESE/ICE", 27), ("TOOTHPASTE", 19)]
>>> scores = {a.lower(): score for answers, score in answer for a in answers.split("/")}
>>> scores
{'lemon': 25, 'apple': 25, 'cheese': 27, 'ice': 27, 'toothpaste': 19}
>>> scores["LemoN".lower()]
25
>>> scores["TooTHPASte".lower()]
19

CodePudding user response:

You can loop over the list like this

inputStr = input("")

for item in answer:
    if inputStr.lower() in item[0].lower().split("/"):
        print(item[1])
        break

CodePudding user response:

input = "LemON"
answer = [("LEMON/APPLE", 25), ("CHEESE/ICE", 27), ("TOOTHPASTE", 19)]
for a in answer:
    if input.upper() in a[0]:
        return a[1] # or print(a[1])

CodePudding user response:

ss = input()
answer = [("LEMON/APPLE", 25), ("CHEESE/ICE", 27), ("TOOTHPASTE", 19)]
for values in answer:
    if ss.upper() in values[0].split('/'):
       print(values[1])
  • Related