如何使我的if语句在while循环中运行一次

时间:2017-12-06 03:36:32

标签: python python-3.x

我试图在python中为类创建一个简化的模拟银行终端应用程序。我试图做到这一点,如果他们有一个支票或储蓄帐户,他们就无法创建一个新帐户。我已经尝试将cacc_check设置为if loop_set和while循环之外的全局变量。当我尝试它时,它会抱怨cacc_check在使用之前尚未初始化。当我在cacc_check =if menu_choice == 1时,每次进入该循环时它都会将其重置为0。我不确定如何初始化cacc_check,以便每次进入该菜单时它都不会被重置为0,并且仍然可以在`if acc_choice == 1和cacc_check中使用!= 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()

我希望能够确保它只能通过一次分配随机帐号的语句运行,否则该帐号将在下次通过时被覆盖。我提前为错误的格式化和优化道歉。我确信有更好的方法来完成我想要做的事情,但我还在学习。任何建议都表示赞赏。

1 个答案:

答案 0 :(得分:0)

您需要将变量移到while循环之前。这样,每次循环运行时,值都不会被重置。尝试类似:

import random

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')

def loopScreen():
    menu_choice = int(input('Please enter a number to select an option'))
    loop_set = 1
    cacc_check = 0
    sacc_check = 0

    while loop_set < 5:
        if menu_choice == 0:
            homeScreen()
        if menu_choice == 1:
            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()


loopScreen()