Hootie Hollister
Hootie Hollister

Reputation: 21

How to get a user input to reference the dictionary Python

I'm having a slight bit of trouble trying in my Python assignment. In the assignment, I am required to create a credit program for a grocery store. It is pretty easy stuff I'm just having trouble on my last part. So, we are required to take 3 user inputs to decide on the items that they would like to purchase. With these inputs, I need to take what the person has typed and turn it into a price. Also, I have been using print('''''')to skip lines, I have found several ways to remedy this but wasn't sure on the correct one.

Here is my assignment so far:

prices = {}  

aa = 5.  
bb = 4.  
cc = 10.  
dd = 3.  
ee = 5.  
ff = 4.  

prices[aa] = 'shrimp'
prices[bb] = 'groundbeef'
prices[cc] = 'tuna'
prices[dd] = 'sodapop'
prices[ee] = 'fruitplate'
prices[ff] = 'spicerack'

print("Hello and welcome to Lakeside Market:") #generic greeting

print('''''') #skip line

deposit = input("How much would you like to deposit into your account?")      #asks for a amount to deposit

print('''''') #skip line

deposit = int(deposit) #makes deposit variable an integer # turns deposit into an integer

bonus = 10 #creates $10 bonus if $20+ is taken from input

accountbalance = deposit + bonus # will be used when the user has entered a correct deposit amount

if deposit < 20: #basic if/else statement. If you put less than 20 it will tell you

    print("I'm sorry that doesn't meet the minimum requirement of $20")

    exit()

else: #if 20+ is entered

    print("Thank you, your funds have been deposited into your account.")

    print('''''') 

    print("Your balance is $",accountbalance) #balance is printed with first-time bonus of 10 added

    print('''''')

    print("We offer: shrimp, groundbeef, tuna, sodapop, fruitplate and spicerack") # here are the different items that can be input
    print("Please enter items as they appear above...") #item1 input

    print('''''')

    item1 = input("To begin, please enter an item name:") #item2 input

    item2 = input("Now, add a second item:")

    item3 = input("Finally, add your last item:") #item3 input

To complete the assignment, I need to turn the user inputs towards the end (item1, item2, item3) into the actual prices of the items from the prices dict. I will then add these items up, and subtract that amount from the accountbalance which will give me a new total on the account. I know this is very basic stuff, but I'm very new to Python and this is the first actual class I've had to take. Thanks for your time and replies in advance

Upvotes: 1

Views: 1992

Answers (1)

Phillip Martin
Phillip Martin

Reputation: 1960

You probably want to organize your items and prices differently for two reasons. The first, as @rajah9 pointed out, when using the prices as your keys you won't be able to map multiple items to the same price:

prices = {}
prices[1] = 'apple'
print prices[1]
''' output '''
'apple'

prices[1] = 'orange'
print prices[1]
''' output '''
'orange'

Second, you probably want easy access to prices from a given item. This is much more difficult if you use the prices as your keys. If you set the items as the keys and the prices as the values in your dictionary, then you can access the prices of each item by calling prices[item].

prices = {}  

aa = 5.  
bb = 4.  
cc = 10.  
dd = 3.  
ee = 5.  
ff = 4.  

prices['shrimp'] = aa
prices['groundbeef'] = bb
prices['tuna'] = cc
prices['sodapop'] = dd
prices['fruitplate'] = ee
prices['spicerack'] = ff

# other stuff here ...

item1 = input("To begin, please enter an item name:") #item2 input

price1 = prices[item1]

item2 = input("Now, add a second item:")

price2 = prices[item2]

item3 = input("Finally, add your last item:") #item3 input

price3 = prices[item3]

Upvotes: 3

Related Questions