我有两个一维列表。 一种是在我的菜单中存放披萨。 其次是存储这些披萨的价格。
但是我不知道如何为每个比萨饼分配此价格,而且,如果用户选择了“ Margerita”比萨饼,则我的程序必须知道Margherita的成本为15美元,并且在选择之后,程序可以打印总计支付比萨饼的金额。请不要注意比萨店的饮料,我只需要解决比萨饼的问题。
pizzeria_menu = ['Margherita', 'Capricossa', 'Salami', 'Wiejska']
pizzeria_drinks = ['Pepsi', 'Mirinda', 'Sprite']
pizza_prices = [20, 15, 25, 30]
def clientOrders():
pizzeria_orders = []
clientPizzaChoice = input('\nJaka pizza dla Ciebie? ')
clientDrinkChoice = input('Jakis napoj do tego? ')
if clientPizzaChoice in pizzeria_menu and clientDrinkChoice in pizzeria_drinks:
pizzeria_orders.append(clientPizzaChoice)
pizzeria_orders.append(clientDrinkChoice)
print('Zamawia pan pizze: ' + clientPizzaChoice + ' i napoj ' + clientDrinkChoice + '.')
elif clientPizzaChoice in pizzeria_menu and clientDrinkChoice not in pizzeria_drinks:
print('Zamowil pan tylko pizze: ' + clientPizzaChoice + '.')
pizzeria_orders.append(clientPizzaChoice)
elif clientPizzaChoice not in pizzeria_menu and clientDrinkChoice in pizzeria_drinks:
print('Zamowil pan tylko napoj: ' + clientDrinkChoice + '.')
pizzeria_orders.append(clientDrinkChoice)
else:
print('Bardzo nam przykro, ze nic Pan nie zamowil. :(')
clientOrders()```
答案 0 :(得分:0)
如果必须使用列表,我可以看到两种方法:
pizzeria_menu = [('Margherita', 20), ('Capricossa', 15), ...]
您可以使用for循环填充此元组列表,并使用 for循环来检索商品价格,这也可以使用列表列表进行。
通过使用枚举函数,您可以在 pizza_menu 中找到存储比萨饼的索引,并使用该索引的值检索其价格。
pizzeria_menu = ['Margherita', 'Capricossa', 'Salami', 'Wiejska']
pizza_prices = [20, 15, 25, 30]
pizza_to_find = "Margherita"
pizza_ix = -1
for ix, pizza_name in enumerate(pizzeria_menu):
if pizza_name == pizza_to_find:
pizza_ix = ix
price_to_find = pizza_prices[pizza_ix]
答案 1 :(得分:0)
如果您确实需要使用列表,则可以使用列表的import matplotlib
matplotlib.use('TkAgg')
from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg
import matplotlib.pyplot as plt
import tkinter as tk
import networkx as nx
import numpy as np
class SimpleTableInput(tk.Frame):
def __init__(self, parent, rows, columns):
tk.Frame.__init__(self, parent)
self._entry = {}
self.rows = rows
self.columns = columns
for row in range(self.rows):
for column in range(self.columns):
index = (row, column)
e = tk.Entry(self)
e.grid(row=row, column=column, stick="nsew")
e.insert(0, '0')
self._entry[index] = e
for column in range(self.columns):
self.grid_columnconfigure(column, weight=1)
self.grid_rowconfigure(rows, weight=1)
def get(self):
result = []
for row in range(self.rows):
current_row = []
for column in range(self.columns):
index = (row, column)
current_row.append(self._entry[index].get())
result.append(current_row)
self.matrix = np.array(result)
return result
class Example(tk.Frame):
def __init__(self, parent, n):
tk.Frame.__init__(self, parent)
self.table = SimpleTableInput(self, n, n)
self.submit = tk.Button(self, text="Submit", command=self.on_submit)
self.table.pack(side="top", fill="both", expand=True)
self.submit.pack(side="bottom")
def on_submit(self):
matrix = self.table.get()
matrix = np.array(matrix)
print(matrix)
def build_graph():
#global table
matrix = table.table.get()
matrix = np.array(matrix)
f = plt.figure(figsize=(5, 4))
plt.axis('off')
G = nx.from_numpy_array(matrix)
pos = nx.circular_layout(G)
nx.draw_networkx(G, pos=pos)
canvas = FigureCanvasTkAgg(f, master=root)
canvas.get_tk_widget().pack(side='bottom', fill='both', expand=1) # ERROR Tk.
def create_matrix():
global table
n = int(e1.get())
table = Example(root, n) # .pack(side="left") ERROR
table.pack(side="left")
b2 = tk.Button(root, text="Build Graph", command=build_graph)
b2.pack(side='bottom')
root = tk.Tk()
b1 = tk.Button(root, text="Number of points", command=create_matrix)
e1 = tk.Entry(root)
e1.insert(0, '0')
b1.pack(side='right')
e1.pack(side='right')
matrix = []
root.mainloop()
方法:请参见data structure doc。
index()
或者如果您更喜欢使用pizzeria_menu = ['Margherita', 'Capricossa', 'Salami', 'Wiejska']
pizza_prices = [20, 15, 25, 30]
clientpizza = input("Order you pizza, please: ")
try:
idx = pizzeria_menu.index(clientpizza)
price = pizza_prices[idx]
print("Your pizza {} costs {:d}".format(clientpizza, price))
except ValueError:
print("Pizza not in the menu")
语句而不是if
try
块:
except
列表索引方法:
在第一个值为x的项的列表中返回从零开始的索引。如果没有此类项目,则会引发ValueError。
因此,在列表中没有重复值的情况下,此方法很好用。
例如,{'{1}}返回if clientpizza in pizzeria_menu:
idx = pizzeria_menu.index(clientpizza)
price = pizza_prices[idx]
print("Your pizza {} costs {:d}".format(clientpizza, price))
else:
print("Pizza not in the menu")
,因为'Salami'是位置2的元素(索引从0开始)。
pizzeria_menu.index('Salami')
这是基本索引,它返回位置2
处的列表元素。
例如,price = pizza_prices[idx]
是idx
。
您应该能够使其适应您的代码。