简体   繁体   English

如何获取用户输入以引用字典Python

[英]How to get a user input to reference the dictionary Python

I'm having a slight bit of trouble trying in my Python assignment. 我在尝试Python任务时遇到了一些麻烦。 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. 因此,我们需要3个用户输入来决定他们想要购买的商品。 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. 另外,我一直在使用print('''''')来跳过行,我找到了几种方法来解决这个问题,但不确定是否正确。

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. 要完成分配,我需要将用户输入结束( item1item2item3 )转换为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. 然后我会添加这些商品,并减去从这一数额accountbalance ,这将给我一个新的总帐户上。 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. 我知道这是非常基本的东西,但我对Python很新,这是我不得不采取的第一个实际课程。 Thanks for your time and replies in advance 感谢您的时间和提前回复

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: 第一个,正如@ rajah9所指出的,当使用价格作为你的钥匙时,你将无法将多个项目映射到相同的价格:

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[item]来访问每个项目的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]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM