-3

Input can be up to (but not always) 20 values between 1-99 separated by 'or' that need to be associated with a string. User input will supply both the value and the "label" that should replace that value in a print-to-text-file output. I also can ask the user for the number of values they supply.

For example: User input:

1 or 2 or 10 or 60

Quantity input:

4

Label input:

Today, Two Days, One Month, One Year

Clarification: the input values do not correspond mathematically to the label string itself and are abstract.

{'1': 'Today', '2': 'Two Days', '10': 'One Month', '60': 'One Year'}

How can I account for user supplied keys and an unknown quantity of pairs?

  • 2
    You should really try to solve this yourself first, and then post a question with relevant code samples. This looks like you are just trying to get help with homework... – thaavik Jul 31 '17 at 19:19

1 Answers1

1

As long as you always know the pairs will align, you don't have to worry about how many there are. Simply split-and-clean, (I'll simply map str.strip for cleaning...) then zip, then pass to the dict type-constructor:

>>> in_1 = "1 or 2 or 10 or 60"
>>> in_2 = "Today, Two Days, One Month, One Year"
>>> dict(zip(map(str.strip, in_1.split('or')), map(str.strip, in_2.split(','))))
{'1': 'Today', '2': 'Two Days', '60': 'One Year', '10': 'One Month'}

This assumes your input is "well-behaved", so, you can reliably split on "or" and commas: ",".

juanpa.arrivillaga
  • 88,713
  • 10
  • 131
  • 172