-4

This problem I'm having is that I cant use that list I have just created and stored inside 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:')
John Lee
  • 5
  • 4

2 Answers2

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!')

Here is your output:

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!

Now you've stated that you want a list, but in your example there is not list, so i'm not sure what you mean, but below is an example of a list of pizzas and attaching a number to each pizza after we access it:

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.")

Above we have a list, which I do not see in your code example called pizza list. If the user selects a pizza within the list we can attach that pizza number to the end of the pizza_ string. We then ask the user how many pizza's they want. The pizza_list can server as your list. Here is the output:

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.
Simeon Ikudabo
  • 2,152
  • 1
  • 10
  • 27
  • That works perfectly to create the list but how do i use that list in an input. Sorry im new to this. I edited the original question and put the code in – John Lee Jun 05 '18 at 08:26
  • What do you mean by "How do a use that list in an input"? You haven't created a list in this example. You simply have a variable. Are you asking how you would add a number to items in a list? For example, you have a list of pizza's and want to attach a number to each pizza? – Simeon Ikudabo Jun 05 '18 at 08:28
  • This was an example i made to demonstrate my problem i have added the code above. – John Lee Jun 05 '18 at 08:29
  • I have used this to create a list that is inside a variable. How do i now use this list inside an input. – John Lee Jun 05 '18 at 08:33
  • You didn't create a list. There's no list in your example. – Simeon Ikudabo Jun 05 '18 at 08:38
  • I know i have used this to create a list in other code. The new code is at the bottom of my question. How do i get that new code to work – John Lee Jun 05 '18 at 08:40
0

I'd suggest

pizza_name = 'pizza_%d' % num

Here the digit will be added to the string.(assuming num is an integer) The same thing can be done for floats and strings as well with minor modifications:

result = 'format_string goes here: %05.2f' % 2.123
foo = 'bar_%s' % 'baz'

The one with the float probably needs a little explanation: the %05.2f means that the float will be formatted to have 5 places in total, padded with leading zeros and 2 decimal digits after the dot.

meissner_
  • 556
  • 6
  • 10