[英]How can I make my program not run in an endless loop once my mouse button is pressed
[英]How to make my if statement run once in a while loop
我正在嘗試為python中的類制作簡化的模擬銀行終端應用程序。 我正在努力做到這一點,以便如果他們有支票或儲蓄帳戶,就無法創建新帳戶。 我嘗試將cacc_check設置為if loop_set
和while循環之外的全局變量。 當我嘗試它時,它抱怨cacc_check
在使用之前尚未初始化。 當我將cacc_check =
in設置為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()
我希望能夠確保它只能在一次分配隨機帳號的語句中運行,否則該帳號將在下次通過時被覆蓋。 對於格式和優化不正確,我深表歉意。 我敢肯定,有更好的方法可以完成我想做的事情,但我仍在學習。 任何建議表示贊賞。
您需要將變量移動到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()
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.