【发布时间】:2021-07-26 15:16:37
【问题描述】:
我目前正在尝试创建一个人们可以“购买”书籍的购物程序。问题是我必须计算购买的最终成本,这是我坚持的。我是否为每个项目创建变量并分配价格?感谢您的帮助!我当前的代码如下。
def showOptions():
print('''Item Number: 1000
Title: Science: A Visual Encyclopedia
Author: Chris Woodford
Genre: Science
Price: $23.99
==============================
Item Number: 1001
Title: My First Human Body Book
Author: Patricia J. Wynne and Donald M. Silver
Genre: Science
Price: $3.99
==============================
Item Number: 1002
Title: The Runaway Children
Author: Sandy Taylor
Genre: Fiction
Price: $3.99
==============================
Item Number: 1003
Title: The Tuscan Child
Author: Rhys Bowen
Genre: Fiction
Price: $9.99
==============================
Item Number: 1004
Title: Learning Python
Author: Mark Lutz
Genre: Programming
Price: $61.99
''')
def displayCart():
shopping_list = open('bookcart.txt')
contents = shopping_list.read()
print('Here is your cart:')
print(contents)
print()
shopping_list.close()
def addItem(item):
shopping = open('bookcart.txt', 'a')
whichbook = input("Please input the item number")
if whichbook == '1000':
shopping.write('''Item Number: 1000
Title: Science: A Visual Encyclopedia
Author: Chris Woodford
Genre: Science
Price: $23.99''')
shopping.close()
print('Item has been added')
print()
elif whichbook == '1001':
shopping.write('''Item Number: 1001
Title: My First Human Body Book
Author: Patricia J. Wynne and Donald M. Silver
Genre: Science
Price: $3.99''')
shopping.close()
print('Item has been added')
print()
elif whichbook == '1002':
shopping.write('''Item Number: 1003
Title: The Tuscan Child
Author: Rhys Bowen
Genre: Fiction
Price: $9.99''')
shopping.close()
print('Item has been added')
print()
elif whichbook == '1003':
shopping.write('''Item Number: 1003
Title: The Tuscan Child
Author: Rhys Bowen
Genre: Fiction
Price: $9.99''')
shopping.close()
print('Item has been added')
print()
elif whichbook == '1004':
shopping.write('''Item Number: 1004
Title: Learning Python
Author: Mark Lutz
Genre: Programming
Price: $61.99''')
shopping.close()
print('Item has been added')
print()
else:
print("Did not understand.")
def checkItem(item):
shopping_list = open('bookcart.txt')
contents = shopping_list.readlines() #puts all the items into a list
shopping_list.close()
item = item + '\n'
if item in contents: #If the specific item is in the list, the function returns true. Otherwise, it returns false.
return True
else:
return False
while True:
menu = int(input('''1. Display Books
2. Add item to cart
3. Show Cart
4. Checkout
5. Quit
Select an option:
'''))
print()
if menu == 1:
showOptions()
elif menu == 2:
addItem()
elif menu == 3:
displayCart()
elif menu == 4:
pass
elif menu == 5:
print("Thank you for shopping!")
break
任何其他代码提示也将不胜感激!我的目标是无论有多少物品被添加到购物车中都能够计算成本,但我不确定如何跟踪它。
【问题讨论】:
-
我建议使用 SQLite 表而不是纯文本文件来存储“购物车”。那么代价就是一个简单的
SUM()函数 -
我将如何使用这张桌子?我从来没有听说过它,它似乎更容易使用。
-
如果我正确理解您的问题,您可以在开头声明一个变量来存储总数并将其设置为 0。并且每次在 @ 中的一个 if 块中添加一个特定价格到该变量987654324@函数执行
-
谢谢!我只是快速浏览了网页并想问,我将如何实现每个项目的信息?还是我只是指定价格并打印其余部分?
标签: python calculation