3
    while n == 1:
        w = input("Input the product code: ")   

Problem is that when a new code for w is entered, w is overwritten. For example, if w = 24, then w = 54, if you print(w) it will just print 54, since it's the latest input. How do you make it so that it prints all inputs for w?

Anya
  • 395
  • 2
  • 13
  • 2
    Use a list for `w` instead and just append each entry? – idjaw Oct 14 '16 at 19:59
  • Could you not print it as you are taking the inputs in? Also the only way you can storage 2 different things in a variable is if it's a set or a class or some sort of containers, unless you concat your values together and store it into the string but that get messy and too much overhead. – MooingRawr Oct 14 '16 at 19:59

3 Answers3

1
inputs = []
for i in range(expected_number_of_inputs):
    inputs.append(input('Product Code: '))
for i in inputs:
    print(i)
Patrick Haugh
  • 59,226
  • 13
  • 88
  • 96
1

Use a container type instead of a single variable. In this case, list() would seem appropriate:

inputs = [] # use a list to add each input value to
while n == 1:
    inputs.append(input("Input the product code: ")) # each time the user inputs a string, added it to the inputs list
for i in inputs: # for each item in the inputs list
    print(i) # print the item

Note: The above code will not compile. You need to fill in the value for the variable n.

Christian Dean
  • 22,138
  • 7
  • 54
  • 87
  • OH THATS HOW IT WORKS – Anya Oct 14 '16 at 20:19
  • @Anya Yes. Although keep in mind that a list is not your only option for a container type. I suggest reading the [up on Python container types](http://stackoverflow.com/questions/11575925/what-exactly-are-containers-in-python-and-what-are-all-the-python-container). – Christian Dean Oct 14 '16 at 20:32
0

You can do this in two separate ways but the way you are trying to do it will not work. You cannot have a variable that is not a list hold two values.

Solution 1: use two variables

w1 = input("value1")
w2 = input("value2")
print(w1)
print(w2)

Solution 2: use a list

w = []
w.append(input("value1"))
w.append(input("value2"))
print(w[0])
print(w[1])
Aiden Grossman
  • 337
  • 5
  • 13