python - 使用变量内的列表
问题描述
我遇到的这个问题是我不能使用我刚刚创建并存储在 current_pizza_list 中的列表。
pizza_1 = ['8.00','Pepperoni']
print('Input 1 for ', pizza_1[1])
current_pizza = input('What pizza would you like:')
current_pizza_list = ('pizza_' + str(current_pizza) + '[1]')
pizza_ammount = input('How many', str(current_pizza_list) ,' pizzas would you like:')
解决方案
num = 5
pizza_name = 'pizza_' + str(num)
print('Our pizza choices are ' + pizza_name + '!')
#What you created above is a variable. That is not a list. Below is a list:
#pizzas = ['pepperoni', 'extra cheese', 'cheese', 'veggie']
current_pizza = input('What pizza would you like: ')
current_pizza_name = ('pizza_' + str(current_pizza) + '[1]')
pizza_ammount = int(input('How many ' + current_pizza_name + "'s would you like?: "))
print('You would like ' + str(pizza_ammount) + ' ' + current_pizza_name + ' pizzas!')
这是您的输出:
Our pizza choices are pizza_5!
What pizza would you like: 5
How many pizza_5[1]'s would you like?: 10
You would like 10 pizza_5[1] pizzas!
现在你已经声明你想要一个列表,但在你的例子中没有列表,所以我不确定你的意思,但下面是一个披萨列表的例子,并在我们访问后为每个披萨附加一个数字它:
pizza_list = [1, 2, 3, 4, 5, 6, 7, 8]
print('Our pizza choices are: ')
for pizza in pizza_list:
print('\t' + str(pizza))
pizza_choice = int(input('Which pizza would you like to select?: '))
if pizza_choice in pizza_list:
current_pizza = 'pizza_' + str(pizza_choice)
else:
print('We do not have that pizza')
pizza_amount = int(input('How many ' + current_pizza + "'s would you like?: "))
print('You would like ' + str(pizza_amount) + ' ' + current_pizza + " pizza's.")
上面我们有一个列表,我在您的代码示例中没有看到它,称为比萨饼列表。如果用户在列表中选择了一个披萨,我们可以将该披萨编号附加到 Pizza_ 字符串的末尾。然后我们询问用户他们想要多少披萨。Pizza_list 可以作为您的列表。这是输出:
Our pizza choices are:
1
2
3
4
5
6
7
8
Which pizza would you like to select?: 5
How many pizza_5's would you like?: 20
You would like 20 pizza_5 pizza's.
推荐阅读
- javascript - 如何在 Grid.MVC 中调用 javascript 函数
- visual-studio - 为什么 NuGet 抱怨 System.ValueTuple (4.3.1)?
- amazon-web-services - 微服务,AWS 中的 docker
- java - 如何在小程序中启用所有权限?
- node.js - Promise 导致 Unexpected 标识符
- sql - 在 MyBatis 中使用 `where 1=2` 查询空列表是一个好习惯吗?
- javascript - 当用户在打开和关闭之间切换时如何检索引导开关值?
- visual-studio - 如何自动更改相似的变量名?
- node.js - 如何使用 JWT 令牌授权 Swagger jsdoc?
- javascript - 自动排列 div,同时使一个 div 变大