Python 在创建列表和调用变量时迷失方向
我目前正在尝试创建一个菜单来为餐厅选择食物,但不知道如何正确显示菜单。我目前对它应该如何工作的非常粗略的想法如下:Python 在创建列表和调用变量时迷失方向,python,Python,我目前正在尝试创建一个菜单来为餐厅选择食物,但不知道如何正确显示菜单。我目前对它应该如何工作的非常粗略的想法如下: name = int(input ('Welcome to Dinos Cupcakes, enter your name to continue ')) cc_list = [ ['1. Chocolate-dipped Maple Puff ($3.50 each)', 1, 3.50], ['2. Strawberry Tw
name = int(input ('Welcome to Dinos Cupcakes, enter your name to continue '))
cc_list = [
['1. Chocolate-dipped Maple Puff ($3.50 each)', 1, 3.50],
['2. Strawberry Twizzler ($2.25 each)', 1, 2.25],
['3. Vanilla Chai Strudel ($4.05 each) ', 1, 4.05],
['4. Honey-drizzled Lemon Dutchie ($1.99)', 1, 1.99]]
print cc_list
while #Selection does not equal 1-4 repeat list
quantity = int(input('How many would you like to purchase of this variety '))
new_quantity = quantity * #Dougnut selection
print (name, 'here is your receipt:'):
我希望最终打印出如下内容:
name = int(input ('Welcome to Dinos Cupcakes, enter your name to continue '))
cc_list = [
['1. Chocolate-dipped Maple Puff ($3.50 each)', 1, 3.50],
['2. Strawberry Twizzler ($2.25 each)', 1, 2.25],
['3. Vanilla Chai Strudel ($4.05 each) ', 1, 4.05],
['4. Honey-drizzled Lemon Dutchie ($1.99)', 1, 1.99]]
print cc_list
while #Selection does not equal 1-4 repeat list
quantity = int(input('How many would you like to purchase of this variety '))
new_quantity = quantity * #Dougnut selection
print (name, 'here is your receipt:'):
姓名这是您的收据:
-------------------------------#巧克力浸枫叶泡芙-------------------------------
总成本:$
谢谢,祝你今天愉快
给你,这是一个“原始”的,请使用Python3.x
name = input ('Welcome to Dinos Cupcakes, enter your name to continue ')
cc_list = [
['1. Chocolate-dipped Maple Puff ($3.50 each)', 1, 3.50],
['2. Strawberry Twizzler ($2.25 each)', 1, 2.25],
['3. Vanilla Chai Strudel ($4.05 each) ', 1, 4.05],
['4. Honey-drizzled Lemon Dutchie ($1.99)', 1, 1.99]]
print(cc_list)
selection = int(input("Please select 1-4"))
while selection not in [1,2,3,4]:
selection = int(input("Please select 1-4"))
quantity = int(input('How many would you like to purchase of this variety '))
total_cost = quantity * cc_list[selection][2]
print(f'{name}, here is your receipt:')
print(f'------------------------------- # {cc_list[selection][0]} -------------------------------')
print(f'Total cost: ${total_cost}')
print('Thank you, have a nice day!')
CMD输出:
Welcome to Dinos Cupcakes, enter your name to continue bill
[['1. Chocolate-dipped Maple Puff ($3.50 each)', 1, 3.5], ['2. Strawberry Twizzler ($2.25 each)', 1, 2.25], ['3. Vanilla Chai Strudel ($4.05 each) ', 1, 4.05], ['4. Honey-drizzled Lemon Dutchie ($1.99)', 1, 1.99]]
Please select 1-41
How many would you like to purchase of this variety 1
bill, here is your receipt:
------------------------------- # 2. Strawberry Twizzler ($2.25 each) -------------------------------
Total cost: $2.25
Thank you, have a nice day!
给你,这是一个“原始”的,请使用Python3.x
name = input ('Welcome to Dinos Cupcakes, enter your name to continue ')
cc_list = [
['1. Chocolate-dipped Maple Puff ($3.50 each)', 1, 3.50],
['2. Strawberry Twizzler ($2.25 each)', 1, 2.25],
['3. Vanilla Chai Strudel ($4.05 each) ', 1, 4.05],
['4. Honey-drizzled Lemon Dutchie ($1.99)', 1, 1.99]]
print(cc_list)
selection = int(input("Please select 1-4"))
while selection not in [1,2,3,4]:
selection = int(input("Please select 1-4"))
quantity = int(input('How many would you like to purchase of this variety '))
total_cost = quantity * cc_list[selection][2]
print(f'{name}, here is your receipt:')
print(f'------------------------------- # {cc_list[selection][0]} -------------------------------')
print(f'Total cost: ${total_cost}')
print('Thank you, have a nice day!')
CMD输出:
Welcome to Dinos Cupcakes, enter your name to continue bill
[['1. Chocolate-dipped Maple Puff ($3.50 each)', 1, 3.5], ['2. Strawberry Twizzler ($2.25 each)', 1, 2.25], ['3. Vanilla Chai Strudel ($4.05 each) ', 1, 4.05], ['4. Honey-drizzled Lemon Dutchie ($1.99)', 1, 1.99]]
Please select 1-41
How many would you like to purchase of this variety 1
bill, here is your receipt:
------------------------------- # 2. Strawberry Twizzler ($2.25 each) -------------------------------
Total cost: $2.25
Thank you, have a nice day!
没有为你做任何事。下面的内容应该足以满足您的需要。请记住,您的cc_列表实际上是列表中的一个列表。要引用元素,您需要引用选择(1,2,3,4),然后引用列表中的元素(0,1,2) 这将允许您提取用户为获取字符串所做的选择以及价格。有了这个,你应该能够找出如何给他们一个总数,并格式化你的输出
name = input('Welcome to Dinos Cupcakes, enter your name to continue ')
cc_list = [
['1. Chocolate-dipped Maple Puff ($3.50 each)', 1, 3.50],
['2. Strawberry Twizzler ($2.25 each)', 1, 2.25],
['3. Vanilla Chai Strudel ($4.05 each) ', 1, 4.05],
['4. Honey-drizzled Lemon Dutchie ($1.99)', 1, 1.99]]
print(cc_list)
selct = int(input("Enter a number"))
order = cc_list[selct+1][0]
cost =str(cc_list[selct+1][2])
print(name + " your stuff")
print("The total cost is " + cost)
没有为你做任何事。下面的内容应该足以满足您的需要。请记住,您的cc_列表实际上是列表中的一个列表。要引用元素,您需要引用选择(1,2,3,4),然后引用列表中的元素(0,1,2) 这将允许您提取用户为获取字符串所做的选择以及价格。有了这个,你应该能够找出如何给他们一个总数,并格式化你的输出
name = input('Welcome to Dinos Cupcakes, enter your name to continue ')
cc_list = [
['1. Chocolate-dipped Maple Puff ($3.50 each)', 1, 3.50],
['2. Strawberry Twizzler ($2.25 each)', 1, 2.25],
['3. Vanilla Chai Strudel ($4.05 each) ', 1, 4.05],
['4. Honey-drizzled Lemon Dutchie ($1.99)', 1, 1.99]]
print(cc_list)
selct = int(input("Enter a number"))
order = cc_list[selct+1][0]
cost =str(cc_list[selct+1][2])
print(name + " your stuff")
print("The total cost is " + cost)
以下是您的修改:
name = input ('Welcome to Dinos Cupcakes, enter your name to continue: ')
cc_list = [
['1. Chocolate-dipped Maple Puff ($3.50 each)', 1, 3.50],
['2. Strawberry Twizzler ($2.25 each)', 1, 2.25],
['3. Vanilla Chai Strudel ($4.05 each) ', 1, 4.05],
['4. Honey-drizzled Lemon Dutchie ($1.99)', 1, 1.99]]
print(cc_list)
hm=0
while hm not in range(1,5):
hm = int(input('Hello {}, Which item (1-4) would you like: '.format(name)))
quantity=0
while quantity not in range(1,100):
quantity = int(input('How many would you like to purchase of this variety: '))
new_quantity = quantity * cc_list[hm-1][2]
print (name, 'here is your receipt: ', new_quantity)
以下是您的修改:
name = input ('Welcome to Dinos Cupcakes, enter your name to continue: ')
cc_list = [
['1. Chocolate-dipped Maple Puff ($3.50 each)', 1, 3.50],
['2. Strawberry Twizzler ($2.25 each)', 1, 2.25],
['3. Vanilla Chai Strudel ($4.05 each) ', 1, 4.05],
['4. Honey-drizzled Lemon Dutchie ($1.99)', 1, 1.99]]
print(cc_list)
hm=0
while hm not in range(1,5):
hm = int(input('Hello {}, Which item (1-4) would you like: '.format(name)))
quantity=0
while quantity not in range(1,100):
quantity = int(input('How many would you like to purchase of this variety: '))
new_quantity = quantity * cc_list[hm-1][2]
print (name, 'here is your receipt: ', new_quantity)
这是我实现你目标的解决方案
cc_list = [
['1. Chocolate-dipped Maple Puff ($3.50 each)', 1, 3.50],
['2. Strawberry Twizzler ($2.25 each)', 1, 2.25],
['3. Vanilla Chai Strudel ($4.05 each) ', 1, 4.05],
['4. Honey-drizzled Lemon Dutchie ($1.99)', 1, 1.99]]
name = input ('Welcome to Dinos Cupcakes, enter your name to continue: ')
print()
for item in cc_list:
print(item)
print()
choice = int(input('Your choice: '))
while choice not in range(1, 5):
choice = int(input('Your choice: '))
quantity = int(input('How many would you like to purchase of this variety: '))
prices = quantity * cc_list[choice-1][-1]
product_name = cc_list[choice-1][0]
print("\n")
print('{} here is your receipt:'.format(name))
print()
print('----------- # {} -----------'.format(product_name))
print()
print("Total cost: {} $".format(prices))
print()
print("Thank you, have a nice day!")
输出:
这是我实现你目标的解决方案
cc_list = [
['1. Chocolate-dipped Maple Puff ($3.50 each)', 1, 3.50],
['2. Strawberry Twizzler ($2.25 each)', 1, 2.25],
['3. Vanilla Chai Strudel ($4.05 each) ', 1, 4.05],
['4. Honey-drizzled Lemon Dutchie ($1.99)', 1, 1.99]]
name = input ('Welcome to Dinos Cupcakes, enter your name to continue: ')
print()
for item in cc_list:
print(item)
print()
choice = int(input('Your choice: '))
while choice not in range(1, 5):
choice = int(input('Your choice: '))
quantity = int(input('How many would you like to purchase of this variety: '))
prices = quantity * cc_list[choice-1][-1]
product_name = cc_list[choice-1][0]
print("\n")
print('{} here is your receipt:'.format(name))
print()
print('----------- # {} -----------'.format(product_name))
print()
print("Total cost: {} $".format(prices))
print()
print("Thank you, have a nice day!")
输出:
在python3.x中试试这个
name = input ('Welcome to Dinos Cupcakes, enter your name to continue : ')
cc_list = [
['1. Chocolate-dipped Maple Puff ($3.50 each)', 1, 3.50],
['2. Strawberry Twizzler ($2.25 each)', 1, 2.25],
['3. Vanilla Chai Strudel ($4.05 each) ', 1, 4.05],
['4. Honey-drizzled Lemon Dutchie ($1.99)', 1, 1.99]]
for x in range(len(cc_list)):
print(cc_list[x])
selection = int(input("Please select 1-4 : "))
while selection not in [1,2,3,4]:
selection = int(input("Please select 1-4 : "))
# array start from possition 0
selection = selection - 1
quantity = int(input('How many would you like to purchase of this variety : '))
total_cost = quantity * cc_list[selection][2]
print(f'{name}, here is your receipt : ')
print(f'------------------------------- # {cc_list[selection][0]} -------------------------------')
print(f'Total cost: ${total_cost}')
print('Thank you, have a nice day!')
输出
Welcome to Dinos Cupcakes, enter your name to continue : Tim
['1. Chocolate-dipped Maple Puff ($3.50 each)', 1, 3.5]
['2. Strawberry Twizzler ($2.25 each)', 1, 2.25]
['3. Vanilla Chai Strudel ($4.05 each) ', 1, 4.05]
['4. Honey-drizzled Lemon Dutchie ($1.99)', 1, 1.99]
Please select 1-4 : 4
How many would you like to purchase of this variety : 10
Tim, here is your receipt :
------------------------------- # 4. Honey-drizzled Lemon Dutchie ($1.99) -------------------------------
Total cost: $19.9
Thank you, have a nice day!
在python3.x中试试这个
name = input ('Welcome to Dinos Cupcakes, enter your name to continue : ')
cc_list = [
['1. Chocolate-dipped Maple Puff ($3.50 each)', 1, 3.50],
['2. Strawberry Twizzler ($2.25 each)', 1, 2.25],
['3. Vanilla Chai Strudel ($4.05 each) ', 1, 4.05],
['4. Honey-drizzled Lemon Dutchie ($1.99)', 1, 1.99]]
for x in range(len(cc_list)):
print(cc_list[x])
selection = int(input("Please select 1-4 : "))
while selection not in [1,2,3,4]:
selection = int(input("Please select 1-4 : "))
# array start from possition 0
selection = selection - 1
quantity = int(input('How many would you like to purchase of this variety : '))
total_cost = quantity * cc_list[selection][2]
print(f'{name}, here is your receipt : ')
print(f'------------------------------- # {cc_list[selection][0]} -------------------------------')
print(f'Total cost: ${total_cost}')
print('Thank you, have a nice day!')
输出
Welcome to Dinos Cupcakes, enter your name to continue : Tim
['1. Chocolate-dipped Maple Puff ($3.50 each)', 1, 3.5]
['2. Strawberry Twizzler ($2.25 each)', 1, 2.25]
['3. Vanilla Chai Strudel ($4.05 each) ', 1, 4.05]
['4. Honey-drizzled Lemon Dutchie ($1.99)', 1, 1.99]
Please select 1-4 : 4
How many would you like to purchase of this variety : 10
Tim, here is your receipt :
------------------------------- # 4. Honey-drizzled Lemon Dutchie ($1.99) -------------------------------
Total cost: $19.9
Thank you, have a nice day!