我试图为python中的一个类创建一个简化的模拟银行终端应用程序。我正在努力使它,如果他们有一个支票或储蓄帐户,他们不能创建一个新的。我已经尝试在if loop_set
和while循环之外将cacu check设置为全局变量。当我尝试这样做时,它会抱怨cacc_check
在使用之前还没有初始化。当我在if menu_choice == 1
中有cacc_check =
时,它会在每次我进入该循环时将其重置为0。我不确定如何初始化cacc_check
,这样它就不会在每次进入该菜单时重置为0,并且仍然可以在“if acc_choice==1和cacc_检查”中使用!=0'这样生成的任何帐号都不会被清除。这就是我现在的困境:
import random
loop_set = 1
while loop_set < 5:
def homeScreen():
print('Welcome to your bank application \n 1. Create Account \n 2. Make a Deposit \n 3. Make a Withdrawal \n'
' 4. View Accounts \n 5. View Transactions \n 6. Transfer Money \n 7. Loans \n 8. Close an Account')
menu_choice = int(input('Please enter a number to select an option'))
if menu_choice == 0:
homeScreen()
if menu_choice == 1:
cacc_check = 0
sacc_check =
print('Creating Account')
name = str(input('Please enter your name: '))
acc_choice = int(input('Type 1 for a checking account and 2 for a savings account'))
if acc_choice == 1 and cacc_check == 0:
cacc_num = random.randint(1000, 9999)
cacc_check += 1
print('Hello ', name, 'your checking account number is', cacc_num)
homeScreen()
if acc_choice == 1 and cacc_num != 0:
print("It looks like you already have a checking account with us")
homeScreen()
if acc_choice == 2 and sacc_check == 0:
sacc_num = random.randint(1000, 9999)
print('Hello ', name, 'your savings account number is', sacc_num)
if loop_set == 1:
loop_set = loop_set + 1
print('loop_set = ', loop_set)
homeScreen()
我希望能够确保它只能运行一次分配一个随机帐号的语句,否则下一次该帐号将被覆盖。我提前为格式和优化的错误道歉。我相信有更好的方法来完成我想做的事情,但我仍在学习。如有任何建议,我们将不胜感激。在
您需要将变量移到
while
循环之前。这样就不会在每次循环运行时重置值。尝试类似于:相关问题 更多 >
编程相关推荐