#python
#питон
Вопрос:
Я пытаюсь создать банковскую программу. Я хочу, чтобы пользователь выбрал что-то (мы скажем » D » для депозита), а затем, когда код завершится, и пользователю будет возвращена общая сумма их новой учетной записи, я хочу начать все сначала Useraction
, и это будет происходить непрерывно, пока пользователь не завершит все свои транзакции.
print('What would you like to do?') UserAction = input("Type 'D' to make depositnType 'W' to make a withdrawalnType 'B' to check your balance: ") while True: if UserAction.upper() == 'D': print('Your current balance is', Userbal, 'Dollars' ) ; sleep(1) DepositAmount = input('How much would you like to deposit? (include cents too): ') pro() print('Your balance has been updated!') UserCash = float(Userbal) float(DepositAmount) Userbal = '{:.2f}'.format(UserCash) print('Your new balance is', Userbal, 'Dollars' ) UserAction if UserAction.upper() == 'W': print('Your current balance is', Userbal, 'Dollars' ) ; sleep(1) WithdrawAmount = input('How much would you like to withdraw? (include cents too): ') pro() print('Your balance has been updated!') UserCash = float(Userbal) - float(WithdrawAmount) Userbal = '{:.2f}'.format(UserCash) print('Your new balance is', Userbal, 'Dollars' ) UserAction if UserAction.upper() == 'B': print('Your current balance is', Userbal, 'Dollars' ) UserAction
Комментарии:
1.
UserAction
Это само по себе ничего не делает. Просто переместитесьUserAction = input("Type 'D' to make depositnType 'W' to make a withdrawalnType 'B' to check your balance: ")
в первую строкуwhile
цикла, чтобы она выполнялась на каждой итерации цикла.
Ответ №1:
Без фактического осуществления депозита , вывода средств и проверки баланса …
def PromptAction(): UserAction = 'P' #priming the variable while UserAction != 'E': UserAction = input('Enter your choice(D-deposit,W-withdraw,B-balance,E-exit:') if UserAction.upper() == 'D': print('All Actions for deposit.') elif UserAction.upper() == 'W': print('All Actions for withdrawal.') elif UserAction.upper() == 'B': print('All actions for checking balance.') elif UserAction.upper() == 'E': print('Exiting...') else: print('{0} is not a valid input.'.format(UserAction))
Исполнение:
gt;gt;gt; PromptAction() Enter your choice(D-deposit,W-withdraw,B-balance,E-exit:D All Actions for deposit. Enter your choice(D-deposit,W-withdraw,B-balance,E-exit:W All Actions for withdrawal. Enter your choice(D-deposit,W-withdraw,B-balance,E-exit:B All actions for checking balance. Enter your choice(D-deposit,W-withdraw,B-balance,E-exit:X X is not a valid input. Enter your choice(D-deposit,W-withdraw,B-balance,E-exit:E Exiting... gt;gt;gt;
Ответ №2:
Для выполнения задачи вам нужен вложенный цикл while. После каждой транзакции вы можете запрашивать у пользователя, хотят ли они, например, внести еще один депозит.
account = [100] print("WELCOME TO PYTHON BAMK") while True: choice = int(input("[1] Balancen[2] Depositn[3] Withdrawln[4] ExitnEnter choice: ")) if choice == 1: while True: print(f'Your balance: {account}') break elif choice == 2: while True: #use nested loop for each block amount = int(input("Enter amount: ")) account.append(amount) print(f"Deposit successful!nBalance: {sum(account)}") another = input("Make another deposit? Y/N: ").capitalize().strip() #prompt user to choose if another == 'Y': another = True else: break elif choice == 3: pass else: print("Thank you for using Python Bank.") break
Ответ №3:
Добавьте входные данные в первую строку тела цикла
print('What would you like to do?') while True: UserAction = input("Type 'D' to make depositnType 'W' to make a withdrawalnType 'B' to check your balance: ") if UserAction.upper() == 'D': print('Your current balance is', Userbal, 'Dollars' ) ; sleep(1) DepositAmount = input('How much would you like to deposit? (include cents too): ') pro() print('Your balance has been updated!') UserCash = float(Userbal) float(DepositAmount) Userbal = '{:.2f}'.format(UserCash) print('Your new balance is', Userbal, 'Dollars' ) UserAction if UserAction.upper() == 'W': print('Your current balance is', Userbal, 'Dollars' ) ; sleep(1) WithdrawAmount = input('How much would you like to withdraw? (include cents too): ') pro() print('Your balance has been updated!') UserCash = float(Userbal) - float(WithdrawAmount) Userbal = '{:.2f}'.format(UserCash) print('Your new balance is', Userbal, 'Dollars' ) UserAction if UserAction.upper() == 'B': print('Your current balance is', Userbal, 'Dollars' ) UserAction