简体   繁体   English

如何使我的if语句偶尔运行一次

[英]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. 我正在尝试为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. 我尝试将cacc_check设置为if loop_set和while循环之外的全局变量。 When I try that it complains that cacc_check hasn't been initialized before it has been used. 当我尝试它时,它抱怨cacc_check在使用之前尚未初始化。 When I have cacc_check = in if menu_choice == 1 it will reset it to 0 every time I enter that loop. 当我将cacc_check = in设置为if menu_choice == 1 ,它将在每次进入该循环时将其重置为0。 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. 我不确定如何初始化cacc_check以免每次我进入该菜单时都不会将其重置为0,并且仍然可以在“ if acc_choice == 1和cacc_check!= 0”中使用它,以便任何帐户生成的号码不会被清除。 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. 您需要将变量移动到while循环之前。 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()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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