Use the code to answer the question. def discount_on_voucher(voucher, amount): discount = 0 if voucher == 'Flat 50': discount = 0.5 * amount elif voucher == 'Flat 30': discount = 0.3 * amount elif voucher == 'Flat 20': discount = 0.2 * amount return discount disc = discount_on_voucher('Flat 75', 1000) print(f'Discount is {disc}') What will be the output of the code? Execute your code using the Trinket. Trinket (1 point) Responses The output will be “Discount is 150.” The output will be “Discount is 150.” The output will be “Discount of 1000.” The output will be “Discount of 1000.” The output will be “Discount is 0.” The output will be “Discount is 0.” The output will be “Discount is 200.”

5 answers

The code provided has a function discount_on_voucher which computes discount based on the voucher type given. The function only recognizes specific voucher strings: 'Flat 50', 'Flat 30', and 'Flat 20'.

In the code, you call the function with the voucher 'Flat 75', which is not recognized by the function. Therefore, the discount variable will remain at its initialized value of 0.

The execution of the function discount_on_voucher('Flat 75', 1000) will result in 0 being returned as there is no matching condition for 'Flat 75'.

Finally, the print statement will output:

Discount is 0

Thus, the correct response is: The output will be “Discount is 0.”

Use the code to answer the question. user = { 'name': 'Bruce', 'Address' : 'Gotham', 'subscription' : { 'title' : 'Student Bundle', 'cost' : 1000 } } print(user.get('subscription').get('title')) What will be the output of the code? Execute the code using the Trinket. Trinket (1 point) Responses Bruce Bruce Student Bundle Student Bundle It will throw a KeyError It will throw a KeyError 1000

The provided code snippet defines a dictionary named user which contains some keys, including a nested dictionary under the key 'subscription'. The nested dictionary has keys 'title' and 'cost'.

Here’s the relevant structure of the dictionary:

user = {
    'name': 'Bruce',
    'Address': 'Gotham',
    'subscription': {
        'title': 'Student Bundle',
        'cost': 1000
    }
}

The line print(user.get('subscription').get('title')) accesses the 'subscription' key in the user dictionary, which retrieves the nested dictionary. Then, it calls .get('title') on that nested dictionary to retrieve the value associated with the key 'title', which is 'Student Bundle'.

Therefore, the output of the code will be:

Student Bundle

Based on the options you provided, the correct response is: Student Bundle.

Use the code to answer the question. user = { 'name': 'Parker', 'Address' : 'London', 'subscription' : { 'title' : 'Student Bundle', 'cost' : 1000, 'valid_till' : { 'month': 'Jan', 'year': 2022, 'date': 2 } } } Demonstrate how can we display the value of key “year” using the get method in the code. (1 point) Responses print(user.get('valid_till').get('subscription').get('year')) print(user.get('valid_till').get('subscription').get('year')) print(user.get('subscription').get('valid_till').get('year')) print(user.get('subscription').get('valid_till').get('year')) print(user.get(subscription).get(valid_till).get(year)) print(user.get(subscription).get(valid_till).get(year)) print(user.['subscription']['valid_till']['year'])

To display the value of the key "year" from the provided dictionary using the .get method, you need to navigate through the dictionary structure correctly.

The structure of your user dictionary is:

user = { 
    'name': 'Parker', 
    'Address': 'London', 
    'subscription': { 
        'title': 'Student Bundle', 
        'cost': 1000, 
        'valid_till': { 
            'month': 'Jan', 
            'year': 2022, 
            'date': 2 
        } 
    } 
}

The correct way to access the "year" value using the get method is:

user.get('subscription').get('valid_till').get('year')

This line works as follows:

  • user.get('subscription') retrieves the dictionary associated with the 'subscription' key.
  • Then, .get('valid_till') retrieves the nested dictionary under the 'valid_till' key.
  • Finally, .get('year') retrieves the value associated with the 'year' key from that nested dictionary.

Among the responses you provided, the correct option is:

print(user.get('subscription').get('valid_till').get('year'))