【发布时间】: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