我遇到的这个问题是我无法使用我刚刚创建并存储在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:')
答案 0 :(得分:1)
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 list的列表。如果用户在列表中选择披萨,我们可以将披萨号附加到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.
答案 1 :(得分:0)
我建议
pizza_name = 'pizza_%d' % num
这里的数字将被添加到字符串中。(假设num是一个整数) 对浮点数和字符串也可以做同样的事情,只需稍作修改:
result = 'format_string goes here: %05.2f' % 2.123
foo = 'bar_%s' % 'baz'
带浮动的那个可能需要一点解释:%05.2f
意味着浮动将被格式化为总共5个位置,用前导零填充,并在点后加2个小数位。