Python中如何将列表中的值分配给列表指定的变量?
问题分析
你代码的核心问题出在最后一个循环:for variable, response in zip(variables, responses): variable = response。这里的variable只是循环迭代出来的字符串(比如'salary'),你给这个字符串变量赋值成response,完全不会影响到全局/局部作用域里名为salary的变量——本质上只是把循环变量variable的内容从字符串换成了数字,和你想要的变量赋值没有任何关系。
另外原代码里的input({question})多了一对大括号,会把question包装成集合传入input,这也是需要修正的小问题。
解决方案
下面提供几种可行的解决方式,按推荐程度排序:
1. 使用字典存储(推荐)
用字典映射变量名和对应的值,比直接创建独立变量更灵活、安全,后续维护和使用也更方便:
questions = ['Please enter your gross annual salary: ', 'Please enter the annual amount of any bonus or commision that appears on your paylip. If not applicable enter 0: ', 'Please enter the annual amount of any benefits in kind that appear on your payslip. If not applicable enter 0: ', 'Please enter your annual total tax credit: ', 'Please enter your annual total pension contribution. If not applicable enter 0: ', 'Do you have any other non tax deductable income? (ie. Holiday purchase schemes etc.) If not applicable enter 0: '] variables = ['salary', 'bonus', 'bik', 'tax_credits', 'pension_contributions', 'other_non_tax_deductibles'] responses = [] for question in questions: while True: try: # 修正原代码的大括号问题 response = float(input(question)) except ValueError: print("Please enter a number") else: responses.append(response) break # 用字典存储变量名和对应值 user_data = dict(zip(variables, responses)) # 后续使用时通过字典访问 print(user_data['salary']) print(user_data['bonus'])
2. 直接修改全局/局部作用域(不推荐)
如果一定要创建独立的变量,可以通过globals()或locals()函数访问作用域字典,直接修改其中的键值对:
questions = ['Please enter your gross annual salary: ', 'Please enter the annual amount of any bonus or commision that appears on your paylip. If not applicable enter 0: ', 'Please enter the annual amount of any benefits in kind that appear on your payslip. If not applicable enter 0: ', 'Please enter your annual total tax credit: ', 'Please enter your annual total pension contribution. If not applicable enter 0: ', 'Do you have any other non tax deductable income? (ie. Holiday purchase schemes etc.) If not applicable enter 0: '] variables = ['salary', 'bonus', 'bik', 'tax_credits', 'pension_contributions', 'other_non_tax_deductibles'] responses = [] for question in questions: while True: try: response = float(input(question)) except ValueError: print("Please enter a number") else: responses.append(response) break # 修改全局作用域的变量 for var_name, value in zip(variables, responses): globals()[var_name] = value # 后续可以直接调用变量 print(salary) print(bonus)
注意:这种方式不推荐,因为可能会意外覆盖已有的变量,代码可读性和安全性都较差。
3. 手动逐个赋值(适合变量少的场景)
如果变量数量不多,直接手动赋值更直观:
questions = ['Please enter your gross annual salary: ', 'Please enter the annual amount of any bonus or commision that appears on your paylip. If not applicable enter 0: ', 'Please enter the annual amount of any benefits in kind that appear on your payslip. If not applicable enter 0: ', 'Please enter your annual total tax credit: ', 'Please enter your annual total pension contribution. If not applicable enter 0: ', 'Do you have any other non tax deductable income? (ie. Holiday purchase schemes etc.) If not applicable enter 0: '] responses = [] for question in questions: while True: try: response = float(input(question)) except ValueError: print("Please enter a number") else: responses.append(response) break # 手动赋值给变量 salary, bonus, bik, tax_credits, pension_contributions, other_non_tax_deductibles = responses # 后续直接调用变量 print(salary)
内容的提问来源于stack exchange,提问作者DeirdreRodgers
相关产品推荐
相关产品推荐

