+ 4

What is wrong with this code

This code is supposed to read a users input and it tells them if the item is in stock or not but it doeent appear to be workign and it’s showing a error Could someone help me: ages = {"apples": "instock", "oranges": "not in stock", "eggs": "in stock"} input(ages("))

2nd Jun 2019, 5:38 PM
Michael Ola
Michael Ola - avatar
3 odpowiedzi
+ 5
ages = {"apples": "instock", "oranges": "not in stock", "eggs": "in stock"} user_input = input() If you know that the user won't enter anything else than "apples", "oranges" or "eggs", you can call the value directly from the dictionary: print(ages[user_input]) Otherwise, it's safer to use the .get method: print(ages.get(user_input, 'not in stock'))
2nd Jun 2019, 6:09 PM
Anna
Anna - avatar
+ 4
A version that should work: ages = {"apples": "instock", "oranges": "not in stock", "eggs": "in stock"} inp = input('what do you like: ') if inp in ages: print(ages.get(inp)) else: print('not found')
2nd Jun 2019, 6:46 PM
Lothar
Lothar - avatar
0
Change line 3 with: print(ages(int(input())))
3rd Jun 2019, 4:12 PM
Roj Serbest