基于变量多次重复输入

问题描述 投票:0回答:1

所以我是python的新手,并且有一个完整的心理块,我可以根据num_qc的值优雅地重复产品名称的输入。

因此,例如,如果num_qc = 4,我希望用户输入nam_prod1,nam_prod2等...据我所知,我不想预先定义这些变量,因为用户只能为num_qc或50输入1?

#report info
num_qc = input('Total QC open: ')
nam_prod = num_qc  * input('Name of unit %s: ' % num_qc)
python variables input arithmetic-expressions
1个回答
0
投票

你必须使用for循环或其他loop cycle,你想要的是:

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

name_prod变量将被每个循环覆盖,你可以使用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)))
© www.soinside.com 2019 - 2024. All rights reserved.