0

So I'm new to python and having a complete mental block as to how I can elegantly repeat this input for the product name based on the value of num_qc.

So for example if num_qc = 4 I would want the user to enter nam_prod1, nam_prod2 etc... As far as my understanding goes, I wouldnt want to pre-define these variables as the user could only enter 1 for num_qc or 50?

#report info
num_qc = input('Total QC open: ')
nam_prod = num_qc  * input('Name of unit %s: ' % num_qc)
Fabián Montero
  • 1,613
  • 1
  • 16
  • 34
Stope
  • 3
  • 2

1 Answers1

0

you have to use a for loop or another loop cycle , what you want is:

num_qc = int(input('Total QC open: '))
for x in range(0,num_qc):
    nam_prod = input('Name of unit %s: ' % (x+1))

the name_prod variable will be overwritten with each cycle, you can use a list:

num_qc = int(input('Total QC open: '))
nam_prod = []
for x in range(0,num_qc):
        nam_prod.append(input('Name of unit %s: ' % (x+1)))
  • I am needing to keep each variable so the second code block will keep the variable? I intend to write this information to a txt file or excel at the end of the program – Stope Sep 17 '18 at 11:14
  • of course, each data will have its own index in the list, with which you can read it (es. nam_prod[0], nam_prod[1], nam_prod[2], .... ) you can use a loop cycle to read all the list. look at the link to learn more about the lists https://docs.python.org/3/tutorial/datastructures.html – Redragon948 Sep 17 '18 at 11:22
  • Thank you for your help! – Stope Sep 17 '18 at 11:59