Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do you store two different inputs from the same variable?

Tags:

python

    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?

like image 757
Anya Avatar asked Jul 31 '26 18:07

Anya


2 Answers

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.

like image 169
Christian Dean Avatar answered Aug 03 '26 09:08

Christian Dean


inputs = []
for i in range(expected_number_of_inputs):
    inputs.append(input('Product Code: '))
for i in inputs:
    print(i)
like image 23
Patrick Haugh Avatar answered Aug 03 '26 08:08

Patrick Haugh



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!