我需要存储6个项目的信息及其各自的长度,宽度和成本值,然后根据用户输入计算出一些值。
在jcusick的帮助下,我现在有了以下代码。
我现在需要帮助注释中标记的项目(#)。
您不需要编写完整的代码 - 指向正确的方向是完美的。
到目前为止,我有:
cost = {}
cost['Themon'] = 450
cost['Larrec'] = 700
cost['Medrec'] = 500
cost['Parallel'] = 825
cost['Suncatch'] = 400
cost['Chantran'] = 975
length = {}
length['Themon'] = 3.2
length['Larrec'] = 3.4
length['Medrec'] = 2.1
length['Parallel'] = 5
length['Suncatch'] = 3
length['Chantran'] = 9
width = {}
width['Themon'] = 2.3
width['Larrec'] = 3
width['Medrec'] = 2
width['Parallel'] = 4
width['Suncatch'] = 3
width['Chantran'] = 6
area = {}
area['Themon'] = 1 # how do i calculate the area (l*w) by referencing the lenght and width dictionary
area['Larrec'] = 1
area['Medrec'] = 1
area['Parallel'] = 1
area['Suncatch'] = 1
area['Chantran'] = 1
#menu
ans=True
while ans:
print("""
Enter 1. to find cheapest garden deck
Enter 2. to display the names of garden decks over a certain lenght
Enter 3. to display the number of garden decks that are available over a certain area
Enter 4. to quit
""")
ans=input("What option would you like? ")
if ans=="1":
print (min(cost, key=cost.get))
elif ans=="2":
input_length = input("\n Please enter minimum deck length between 2 and 15 metres")
print # i need to display names of the decks greater than the user input length
elif ans=="3":
input_area = input("\n Please enter deck area in metres squared between 4 and 80")
print # i need to display the number of garden decks that are greater that the user input area
elif ans=="4":
print("\n Thank you for using Penny's Decking")
else:
print("\n Not a valid choice")
答案 0 :(得分:1)
我可能会考虑为您感兴趣的每个变量(长度,宽度,成本)创建dictionary。然后,您可以始终根据用户的答案查询特定字典:
cost = {}
cost['Themon'] = 450
cost['Larrec'] = 700
...
print (min(cost, key=cost.get))
# 'Themon'
答案 1 :(得分:1)
我认为对于像这样的一小段代码,大熊猫可能有些过分。对于您的特定示例,数组将是一种更快的存储方法。尽管如此,我还是会说,列表可能是处理这种特定规模的更加pythonic方式。在这里,你的性能不会受到任何影响,现在它的可读性稍微高一些,而不是使用数组。
为了进一步阅读,有时一个命名元组可以在您保存和访问记录的大型示例中提供帮助: https://docs.python.org/3/library/collections.html#collections.namedtuple
答案 2 :(得分:0)
我建议你研究一下Pandas并使用DataFrames更具体地实现它,因为它们非常适合你正在做的事情。就像其他人提到的那样。确保缩进格式正确。如果您不想使用DataFrames,那么imho非常适合您正在做的事情。我建议创建一个值字典并引用它们。