简体   繁体   中英

How to make my if statement run once in a while loop

I'm trying to make a simplified mock banking terminal application for a class in python. I'm trying to make it so that if they have a checking or savings account they cannot create a new one. I have tried setting cacc_check to a global variable in both the if loop_set and outside of the while loop. When I try that it complains that cacc_check hasn't been initialized before it has been used. When I have cacc_check = in if menu_choice == 1 it will reset it to 0 every time I enter that loop. I'm not sure how to initialize cacc_check so that it won't get reset to 0 every time I enter that menu and still be able to be used in the `if acc_choice == 1 and cacc_check != 0' so that any account number generated is not wiped out. This is where I'm stuck right now:

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

I'd like to be able to ensure that it can only run through the statement assigning a random account number once, or else that account number will get overwritten next time it goes through. I apologize in advance for bad formatting and optimization. I'm sure there are better ways to accomplish what I'm trying to do but I'm still learning. Any advice is appreciated.

You need to move the variables to before the while loop. That way the values don't get reset each time the loop runs. Try something like:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM