+ 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("))
4 Antworten
+ 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'))
+ 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')
0
Change line 3 with:
print(ages(int(input())))