无法从转换的冻结集python 3获取值
我正在自学Python(之前没有编程经验),我正在尝试这个问题: 要求用户输入他们想要的任意多个银行账号,并将其存储在一个列表中。用户输入完信息后,将列表转换为冻结集并打印出来 这是我的代码:无法从转换的冻结集python 3获取值,python,loops,set,Python,Loops,Set,我正在自学Python(之前没有编程经验),我正在尝试这个问题: 要求用户输入他们想要的任意多个银行账号,并将其存储在一个列表中。用户输入完信息后,将列表转换为冻结集并打印出来 这是我的代码: # create global variables b_accounts = [] fzb_accounts = frozenset() # Create add account function def addAccount(account): b_accounts.append(account
# create global variables
b_accounts = []
fzb_accounts = frozenset()
# Create add account function
def addAccount(account):
b_accounts.append(account)
print('Account number: {} has been added'.format(account))
return b_accounts
# create covert from a list to a frozenset function
def convertFz():
if b_accounts:
globals()['fzb_accounts'] = frozenset(b_accounts)
return fzb_accounts
else:
print('List of account does not exist!')
# create show account function
def showAccount():
convertFz()
if fzb_accounts:
#print('Here your enique entered accounts:{}'.format(fzb_accounts))
for acc in fzb_accounts:
print(acc)
else:
print('No account!')
# create main function
def main():
done = False
while not done:
ans = input('Please select add/show/quit account: ').lower()
if ans == 'add':
account = input('Enter account number: ')
addAccount(account)
elif ans =='show':
showAccount()
elif ans =='quit':
done = True
print('Bye!')
else:
print('Invalid option')
main()
我想添加以下帐号:
- 1234
- 12345
- 1234
- 1234
- 12345
谢谢大家,代码更新了,工作正常 如果你想把fzbęu accounts
fzbęu accounts作为一个全局变量,你需要这样声明。嗨@Błotosmętek,我相信fzbęu accounts是一个全局变量,正如我在开始时声明的那样:fzbęu accounts=frozenset()。fzbĚu accounts=frozenset(Bęaccounts)
这一行创建了一个新的局部变量(不修改全局变量)不,您没有将其声明为全局变量,您只是初始化了一个变量。请参阅@błotosmętek,谢谢,更新了我的代码,它可以正常工作