根据用户输入创建菜单



所以我一直在尝试编写一个类似于菜单创建程序的代码:

1#接受用户输入,定义产品数量,并命名

2#从用户中获取产品名称后单独分配价格

3#完成后显示菜单

我的代码问题是,在函数1完成后,我不能转到函数2,程序刚好在第一个循环之后由于这里的编辑器,缩进是错误的,它只是改变了我的代码位置,我不知道为什么

我是编码和python语言的初学者这就是我的代码在多次尝试和研究后的表现

def functions():
maxlengh = input("how many products in your card ? : ")
maxlengh = int(maxlengh)
select_function = input("press 1 to add product names to the menu or 2 to assign prises : ")
select_function = int(select_function)
products = []
while select_function == 1 and len(products) != maxlengh :
items = input("enter product name : ")
items= items.split()
products.append(items)
if len(products) == maxlengh :
select_function == 2
prise = []
while select_function ==2 and len(prise) != maxlengh :
items = input("enter product prise : ")
items = items.split()
prise.append(items)
menu = dict(zip(products,prise))
print(menu  )

这里试试这个:在Python中,缩进非常重要,而您的一些缩进有点偏离。这应该有效。祝你好运

def functions():
max_length = int(input("how many products in your card ? : "))
select_function = input("press 1 to add product names to the menu or 2 to assign prices : ")
select_function = int(select_function)
products = []
while select_function == 1 and len(products) != max_length :
items = input("enter product name : ")
items = items.split()
products.append(items)
if len(products) == max_length :
select_function == 2
price = []
while select_function ==2 and len(price) != max_length :
items = input("enter product price : ")
items = items.split()
price.append(items)
menu = dict(zip(products,price))
print(menu)

您可以使用

dict

这是您的代码:

def functions():
maxlengh = int(input("how many products in your card ? : "))
products = [] #[{name:"name",price:"price",}]
for num in range(maxlengh):
product_name=input("inter product name: ")
product_price=input("inter product price: ")
product={"name":product_name,"price":product_price}
products.append(product)
print(products)
return  products

函数((

最新更新