0

I have the task to do but I don't know how can I start. In output I need to give back day name and number.

I know how to get the day name but I can't find how to get number either. Could you suggest me something.

Data for the task:

data = (
data = """
monday;1250
tuesday;1405
wednesday;1750
thursday;1100
friday;0800
saturday;1225
sunday;1355
"""

My uncompleted code:

day = input("Insert day: ").lower()

if day in data:
    print("""The day is "{}"\nThe number is: """.format(dzien))

The output should looks like:

The day is "day name"
The number is "number"

3 Answers 3

4

You could parse your data into a dictionary and then look up the corresponding number:

data = """
monday;1250
tuesday;1405
wednesday;1750
thursday;1100
friday;0800
saturday;1225
sunday;1355
"""

data = {day: number for day, number in [line.split(';') for line in data.strip().split('\n')]}

day = input("Insert day: ").lower()

if day in data:
    print(f"""The day is "{day}"\nThe number is: {data[day]}""")
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks it works. I have one more question. Does it possible to assign that "data[day]" to variable? I need to do some simply calculation with that number but when I create variable value = data[day] it does not work. I get error message
Please mark an answer as correct then to conclude this particular question. If you have additional questions, it would be best to create a new question with an equally nice write up (including the error message). Right now, I can only tell you that an assignment in the form of user_number = data[day] should work if used in the right place.
0

Use a dictionary. Dictionaries allow to organize data in key-value format, so you can access to the information binded to the day with data[day]. Change the code to:

data = {
    'monday': 1250,
    'tuesday': 1405,
    'wednesday': 1750,
    'thursday': 1100,
    'friday': 800,
    'saturday': 1225,
    'sunday': 1355
}

day = input("Insert day: ").lower()

if day in data:
    # get number associated with day
    day_number = data[day] 
    # print output
    print('The day is "{}".\nThe number is "{}".'.format(day, day_number))

It will print:

Insert day: monday
The day is "monday".
The number is "1250".

2 Comments

Thank you. I know how to deal with it if it would be the dictionary but the task is different.
Ok sorry i missunderstood your question. To convert your text to dictionary, use: data = dict([cur_el.split(';') for cur_el in data.strip().split('\n')])
0

Convert data into dictionary:

data = {'monday' : '1250', 
        'tuesday': '1405', 
        'wednesday': '1750', 
        'thursday':'1100',
        'friday': '0800',
        'saturday':'1225',
        'sunday':'1355'}


day = input("Insert day: ").lower()

if day in data:
   print(f'The day is "{day}"\nThe number is: "{data[day]}"')

1 Comment

Thank you. I know how to deal with it if it would be the dictionary but the task is different.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.