我正在購物車上練習,其中包括用戶輸入的專案串列。這是怎么回事:
cart_items = []
price_items = []
print("Welcome to the Shopping Cart Program!")
print()
def menu():
print("1. Add a new item")
print("2. Display the content of the shopping cart")
print("3. Remove an item of the shopping cart")
print("4. Compute total of the items in the shopping cart")
print("5. Quit")
def display_content():
for i in range(0, len(cart_items)):
print(f"{cart_items[i]} - {price_items[i]}")
menu()
option = int(input("Please, enter an action: "))
while option != 5:
if option == 1:
new_item = input("What item would you like to add? ")
price_item = float(input(f"What is the price of the {new_item}? "))
cart_items.append(new_item)
price_items.append(price_item)
print(f"{new_item} has been added to the cart.")
if option == 2:
print("The content of the shopping cart are:")
display_content()
if option == 3:
new_item = input("Which item would you like to remove? ")
temp = []
for item in cart_items:
if item[new_item] != new_item:
temp.append(item)
if option == 4:
print('\n\n')
summation = 0
for item in cart_items:
for product in cart_items:
if product['id'] == item['id']:
summation = summation \
(product['price'] * item['quantity'])
break
print(f'The total price of the items in the shopping cart is ${0}'.format(summation))
elif option >= 5:
print("Invalid option, please, try again.")
print("Thank you for using the shopping cart program. Good bye!")
因此,對于第一個選項“1”,回圈繼續要求添加新專案。
我還撰寫了第一個代碼,該代碼通常回傳到看起來相似但更簡單的def 選單,并且當前按預期作業:
cart_items = []
price_items = []
print("Welcome to the Shopping Cart Program!")
print()
def menu():
print("1. Add a new item")
print("2. Display the content of the shopping cart")
print("3. Quit")
def display_content():
print(cart_items[0], price_items[0])
for i in range(0, len(cart_items)):
print(f"{cart_items[i]} - {price_items[i]}")
menu()
option = int(input("Please, enter an action: "))
while option != 3:
if option == 1:
new_item = input("What item would you like to add? ")
price_item = float(input(f"What is the price of the {new_item}? "))
print(f"{new_item} has been added to the cart.")
cart_items.append(new_item)
price_items.append(price_item)
elif option == 2:
for i in range(len(cart_items)):
items = cart_items
print("The content of the shopping cart are:")
display_content()
else:
print("Invalid option, please, try again.")
print()
menu()
option = int(input("Please, enter an action: "))
print("Thank you for using the shopping cart program. Good bye!")
不確定我是否能夠在沒有幫助的情況下完成這個程式。我不知道,一開始看起來不錯,但最后,在 YT 上舉一些例子并不能幫助我完成程式。總災難:(
uj5u.com熱心網友回復:
您必須在-loopmenu()
內部運行。input()
while
在第一個版本中,您沒有在while
-loop 中使用它,這會產生問題。
但是在第二個版本中,您將它放在while
-loop 中并且它可以作業。
更簡單的版本
while True:
menu()
option = int(input("Please, enter an action: "))
if option == 5:
break # exit loop
# ... check other options ...
編輯:
未測驗
# --- functions ---
def menu():
print("1. Add a new item")
print("2. Display the content of the shopping cart")
print("3. Remove an item of the shopping cart")
print("4. Compute total of the items in the shopping cart")
print("5. Quit")
def add_item():
name = input("What item would you like to add? ")
price = float(input(f"What is the price of the {name}? "))
cart.append( [name, price] )
print(f"{name} has been added to the cart.")
def display_content():
print("The content of the shopping cart are:")
for name, price in cart:
print(f"{name} - {price}")
def remove_item():
selected_name = input("Which item would you like to remove? ")
temp = []
for name, price in cart:
if name != selected_name:
temp.append( [name, price] )
cart = temp
def total_sum():
summation = 0
for name, price in cart:
summation = price
print(f'The total price of the items in the shopping cart is ${summation}')
# --- main ---
cart = [] # keep pairs [name, price]
print("Welcome to the Shopping Cart Program!")
print()
while True:
menu()
option = int(input("Please, enter an action: "))
if option == 5:
break
elif option == 1:
add_item()
elif option == 2:
display_content()
elif option == 3:
remove_item()
elif option == 4:
total_sum()
else:
print("Invalid option, please, try again.")
print("Thank you for using the shopping cart program. Good bye!")
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/496474.html
上一篇:將類更改為功能組件
下一篇:返回列表