【问题标题】:How can I use a while loop to append a dictionary with an arbitrary number of keys and multiple values from user inputs如何使用 while 循环从用户输入中附加任意数量的键和多个值的字典
【发布时间】:2020-05-06 09:12:48
【问题描述】:
print('Scenario Analysis')

profit_dict = {}

while True:
    item1= input ('What is the item: ')   
    price_good= int (input('What is the expected profit of {}, in the best case scenario: '.format(item1)))
    price_bad = int( input ('What is the expected profit of {}, in the worst case scenario: '.format(item1)))
    user_choice= input('Do you have anymore items: ')
    if user_choice in ['Yes','yes','y','Y']:
            pass
    else:
            break

profit_dict[item1]=price_good,price_bad

#EVERYTHING FROM HERE ON IS NOT RELEVANT DIRECTLY TO QUESTION BUT PROVIDES CONTEXT

print('This is your best profit outcome: ')
for values in good_dict.values():
    total_list=(max(values))
    print(total_list)

print('This is your worst profit outcome: ')

我知道变量的使用会不断取代字典,但这是我展示我的目标的最佳方式。可能使用函数而不是 while 循环可能会有所帮助,但我不确定。

提前感谢您的任何回复。

【问题讨论】:

  • 您能否指定所需的输出和示例输入,以便更清楚地理解?据我了解,您需要在字典中附加值,直到用户输入 Yes ?如果他们输入否,你应该打破然后简单地打印字典?
  • 当然,总的来说,我想创建一个程序,在最好和最坏的情况下为用户提供总利润。我想使用重复的 while 循环,以便用户可以将任意数量的项目添加到字典中,例如'product A': (100,50), 'product b' : (10, -5) 然后最后我将使用 max 和 min 来计算好和坏的利润结果
  • 为什么不在while循环中插入字典?
  • 如果我是正确的,我相信每次循环重新启动时,变量名的使用都会不断替换附加到字典中的任何内容,所以我正在寻找解决这个问题的方法

标签: python dictionary input while-loop append


【解决方案1】:

这样的事情是否符合您的期望:

print('Scenario Analysis')

profit_dict = {}

while True:
   item1= input ('What is the item: ')
   price_good= int (input('What is the expected profit of {}, in the best case scenario: '.format(item1)))
   price_bad = int( input ('What is the expected profit of {}, in the worst case scenario: '.format(item1)))

   if item1 in profit_dict.keys():
       profit_dict[item1]['good'].append(price_good)
       profit_dict[item1]['bad'].append(price_bad)

   else:
       profit_dict[item1]={'good':[price_good],'bad':[price_bad]}

   user_choice= input('Do you have anymore items: ')
   if user_choice in ['Yes','yes','y','Y']:
        pass
   else:
        break

=> 你会得到一个字典的字典: {'Item1':{'good':[1,2,3],'bad':[0,0,1]},'Item2':{'good':[10,20,30],'bad ':[1,2,3]}, ,...,'ItemX':{'good':[10,20,30], 'bad':[1,2,3]}}

然后您可以尝试使用以下内容调用打印:

print(f'This is your best profit outcome: {max(profit_dict[item]['good'])}')
print(f'This is your worst profit outcome: {min(profit_dict[item]['bad'])}')

编辑: 不确定是否理解您的评论: 如果您正在寻找特定项目:

item='item1'
print(f'This is your best profit outcome: {max(profit_dict[item]['good'])}')

如果项目未知 您可以通过迭代探索字典:

for key, value in profit_dict.items():
    for key2, value2 in value.items():
        print(f"This is your best profit outcome for {key}:{max(profit_dict[item]['good'])}")

它会打印你: 这是您对 Item1:3 的最佳利润结果 这是您对 Item2:30 的最佳利润结果 ... 这是 ItemX 的最佳利润结果:30

如果你想知道“好”更大的项目,这个解决方案不是最好的,但你仍然可以进行迭代:

previous_best_value=0
for key, value in profit_dict.items():
    for key2, value2 in value.items():
        best_value=max(profit_dict[key]['good'])
        if best_value>previous_best_value:
            previous_best_value=best_value
            best_item=key

print(f"The item:{key} has the best outcome {previous_best_value}")

希望你会找到你所期望的。

【讨论】:

  • 嘿,谢谢您的回复,您编写的循环代码很棒,但是,我如何将字典中的所有最大值称为 'good' 或 'bad' ,因为项目未定义
  • 嘿,再次感谢,但我的问题比这简单得多,我会尝试更详细的。当调用字典来打印利润情况的好坏时,例如 max(profit_dict[items]['good']))) 名称 items 没有分配给字典中的任何键,所以我不能调用值。最终,我想调用每个键的所有值并将它们总计。所以也许调用好的值并将它们附加到列表中?
【解决方案2】:
profit_dict = {}

while True:
    item1= input ('What is the item: ')   
    price_good= int (input('What is the expected profit of {}, in the best case scenario: '.format(item1)))
    price_bad = int( input ('What is the expected profit of {}, in the worst case scenario: '.format(item1)))
    user_choice= input('Do you have anymore items: ')
    profit_dict[item1]=[price_good,price_bad]    
    if user_choice in ['Yes','yes','y','Y']:
            continue
    else:
            break

print(profit_dict)

这就是你要找的吗?这只是继续添加字典,直到用户输入是。

【讨论】:

猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-12-04
  • 2020-06-08
  • 1970-01-01
  • 1970-01-01
  • 2014-01-03
  • 2014-10-16
相关资源
最近更新 更多