所以我一直在嘗試撰寫一個像選單創建程式一樣作業的代碼:
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 )
uj5u.com熱心網友回復:
在這里試試這個:在 Python 中,縮進非常重要,而你的一些縮進有點不對勁。這應該有效。祝你好運!
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)
uj5u.com熱心網友回復:
您可以使用
dic 她是你的代碼:
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
職能()
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/448617.html
標籤:Python python-3.x 列表
