0

I am having a problem where the user enters various codes , into 1 variable and then i want to split these input to make it into a list.However , I have realized that my variable only takes in the last input meaning the list comes out with only one code and not several. How do I make a variable store multiple inputs ?

while True:
    itemsneeded = input("How many items do you need?")
    if itemsneeded.isnumeric() and int(itemsneeded) <= 5:
        break
    GTIN = ''
    count = 0
    while count < int(itemsneeded):
        GTIN = (input('Please enter all GTIN-8 for all items'))
        if GTIN.isnumeric() and len(GTIN) == 8:
            Num0 = int(GTIN[0]) * 3
            Num1 = int(GTIN[1])
            Num2 = int(GTIN[2]) * 3
            Num3 = int(GTIN[3])
            Num4 = int(GTIN[4]) * 3
            Num5 = int(GTIN[5])
            Num6 = int(GTIN[6]) * 3
            Num7 = int(GTIN[7])
            total2 = (Num0 + Num1 + Num2 + Num3 + Num4 + Num5 + Num6 + Num7)
            if total2 % 10 == 0:
                print(GTIN)
                if GTIN in open('read_it.txt').read():
                    print('entered GTIN is valid')
                else:
                    print('The code entered is invalid')
                    print('Please renter this code')

            count += 1
        else:
            print("The entered GTIN-8 codes are incorrect")
            print(GTIN)
    lst = GTIN.split()
    print(lst)

And I can not use this (Two values from one input in python?) becuase I do not know how many items the user wants , the users input can vary from 1 item to 5.

Community
  • 1
  • 1

2 Answers2

1

Create an empty list and then use list.append inside you loop. What this will do is add each new entry to the end of your list.

GTIN=''
#count=0
items = [] # replaces count = 0
#while count<int(itemsneeded):
while len(items) < int(itemsneeded): # replaces other while condition
    ...
        ...
        #count += 1
        items.append(GTIN) # replaces count += 1
    ...

print(items)

You should also avoild using list as the variable name as you overwrite the built-in list method.

Also your code doesn't quite work for invalid inputs. If the code they enter is incorrect then it still increments as if a valid item has been added. You should move items.append(GTIN) to inside the if statement where you check the GTIN is valid.

Steven Summers
  • 5,079
  • 2
  • 20
  • 31
  • Thank you very much for your answer. –  Aug 23 '16 at 12:47
  • Thank you very much for your answer. I am new to this .append() function , can you explain what it does and why do you check the length of item ? I won't use list as a variable ever again ;). Also , I just tested my program , it does only expect codes from my text file , as you can see my program reads my text file to see if the user input matches.If you try to input anything else , it will just output the code is incorrect and loop the question. –  Aug 23 '16 at 12:56
  • Instead of explaining about lists, I think it would be best to read through some of the [docs](https://docs.python.org/3/tutorial/datastructures.html#) or python tutorials. They're quite simple to understand so just play around with them in IDLE until you do. – Steven Summers Aug 23 '16 at 14:12
0

Perhaps it will help you:

nums = input().split()
int_nums = [ int(x) for x in nums ]
dannyxn
  • 422
  • 4
  • 16
  • I added .split() on to line 8 , the problem is now i am getting " 'list' object has no attribute 'isnumeric' " , obviously i am using isnumeric() to validate the users input is a number. I have also tried putting int on line 8 instead of using isnumeric() , but I still get the same error. –  Aug 23 '16 at 12:42