当前位置: 代码迷 >> python >> 提款时出现错误消息,当输入的金额大于或小于帐户余额时,无法使其正常运行
  详细解决方案

提款时出现错误消息,当输入的金额大于或小于帐户余额时,无法使其正常运行

热度:102   发布时间:2023-07-14 09:52:17.0

我是python的新手,并且确实编码。 我正在尝试创建一个ATM类型的程序,让您进行存款和取款。 我输入了我收到的错误消息。 老实说,我只是不知道从这里出发。

另外,如何确保%的提款额能被10整除?

任何帮助表示赞赏!

File "/Users/jess/PycharmProjects/assignment2/assignment 1.py", line 54, in <module>
    r = input("Are you sure you want to deposit ", deposit, "? [Y/N]")
TypeError: [raw_]input expected at most 1 arguments, got 3

码:

#display welcome message
print("Welcome to a Virtual Credit Union ATM!")


counter = 0
while counter < 3:
    pin = input("Please input your 4-digit PIN: ")

if pin == 9876:
    print("Correct PIN, will continue to menu")
    break

print("Incorrect PIN, please try again")
counter += 1

if counter == 3:
    print("You're locked out, Goodbye")
else:
    print("Onto Menu options")


def menu():       ## Your menu design here
    print "1. View Account Balance"
    print "2. Withdraw cash"
    print "3. Make a deposit"
    print "4. Exit"


loop = True

while loop:     ## While loop which will keep going until loop =False
menu()    ## Displays menu
choice = input("Enter your choice [1-4]: ")

if choice == 1:
    account_balance = (int, 'Your account balance is: $500')
    print(account_balance)

elif choice == 2:
    print "Withdraw Cash, must be multiple of 10"
    withdraw = int(input('How much do you want to Withdraw?:'))

    if withdraw > account_balance:
        print("Insufficient funds")
        withdraw = int(input("Enter new amount to Withdraw: "))

    elif withdraw <= account_balance:
        balance = account_balance - withdraw
        print (' New Balance :', balance)

elif choice == 3:
    print "Make Deposit"
    deposit = int(input('How much do you want to Deposit?:'))
    r = input("Are you sure you want to deposit ", deposit, "? [Y/N]")
    if choice == y:
        balance = account_balance + deposit
        print (' New Balance:', balance)

    elif choice == n:
        deposit2 = input("Enter the amount you want to deposit")
        balance = account_balance + deposit2
        print (' New Balance:', balance)

elif choice == 4:
    print "Exit, Goodbye!"
    break

    loop = False # This will make the while loop to end as not value of loop is set to False
else:
    # Any integer inputs other than values 1-5 we print an error message
    raw_input("Wrong option selection. Enter any key to try again..")

尝试此修复程序缩进,使其更美观:

#display welcome message
print("Welcome to a Virtual Credit Union ATM!")


counter = 0
while counter < 3:
    pin = input("Please input your 4-digit PIN: ")

    if pin == 9876:
        print("Correct PIN, will continue to menu")
        break

    print("Incorrect PIN, please try again")
    counter += 1

if counter == 3:
    print("You're locked out, Goodbye")
else:
    print("Onto Menu options")


def menu():       ## Your menu design here
    print "1. View Account Balance"
    print "2. Withdraw cash"
    print "3. Make a deposit"
    print "4. Exit"


loop = True

while loop:     ## While loop which will keep going until loop =False
    menu()    ## Displays menu
    choice = input("Enter your choice [1-4]: ")

    if choice == 1:
        account_balance = (500,'Your account balance is: $500')
        print(account_balance[1])


    elif choice == 2:
        print "Withdraw Cash, must be multiple of 10"
        withdraw = input('How much do you want to Withdraw?:')

        while withdraw > account_balance[0]:
            print("Insufficient funds")
            withdraw = input("Enter new amount to Withdraw: ")



        balance = account_balance[0] - withdraw
        print ' New Balance :', balance
        account_balance=(balance,'Your account balance is: $%s'%balance)

    elif choice == 3:
        print "Make Deposit"
        deposit = input('How much do you want to Deposit?:')
        r = raw_input("Are you sure you want to deposit "+str(deposit)+"? [Y/N]")
        if choice == 'y':
            balance = account_balance + deposit
            print (' New Balance:', balance)

        elif choice == 'n':
            deposit2 = input("Enter the amount you want to deposit")
            balance = account_balance + deposit2
            print (' New Balance:', balance)

    elif choice == 4:
        print "Exit, Goodbye!"


        loop = False # This will make the while loop to end as not value of loop is set to False
    else:
        # Any integer inputs other than values 1-5 we print an error message
        raw_input("Wrong option selection. Enter any key to try again..")

输出示例:

Welcome to a Virtual Credit Union ATM!
Please input your 4-digit PIN:  1234
Incorrect PIN, please try again
Please input your 4-digit PIN:  9876
Correct PIN, will continue to menu
Onto Menu options
1. View Account Balance
2. Withdraw cash
3. Make a deposit
4. Exit
Enter your choice [1-4]:  1
Your account balance is: $500
1. View Account Balance
2. Withdraw cash
3. Make a deposit
4. Exit
Enter your choice [1-4]:  2
Withdraw Cash, must be multiple of 10
How much do you want to Withdraw?: 5000
Insufficient funds
Enter new amount to Withdraw:  5600
Insufficient funds
Enter new amount to Withdraw:  150
 New Balance : 350
1. View Account Balance
2. Withdraw cash
3. Make a deposit
4. Exit
Enter your choice [1-4]:  3
Make Deposit
How much do you want to Deposit?: 124
Are you sure you want to deposit 124? [Y/N] y
1. View Account Balance
2. Withdraw cash
3. Make a deposit
4. Exit
Enter your choice [1-4]:  4534
Wrong option selection. Enter any key to try again.. blah
1. View Account Balance
2. Withdraw cash
3. Make a deposit
4. Exit
Enter your choice [1-4]:  5
Wrong option selection. Enter any key to try again.. 2
1. View Account Balance
2. Withdraw cash
3. Make a deposit
4. Exit
Enter your choice [1-4]:  4
Exit, Goodbye!
# comments:
# at some places you are using print function while at other you are using print as statement
# 1 == '1' false

#display welcome message
print("Welcome to a Virtual Credit Union ATM!")


counter = 0
while counter < 3:
    pin = int(input("Please input your 4-digit PIN: ")) # int()

    if pin == 9876:
        print("Correct PIN, will continue to menu")
        break
    else:
        print("Incorrect PIN, please try again")

    counter += 1

if counter == 3:
    print("You're locked out, Goodbye")
else:
    print("Onto Menu options")



def menu():       ## Your menu design here
    print("1. View Account Balance")
    print("2. Withdraw cash")
    print("3. Make a deposit")
    print("4. Exit")


account_balance = 500


loop = True
while loop:     ## While loop which will keep going until loop =False
    menu()    ## Displays menu
    choice = int(input("Enter your choice [1-4]: "))

    if choice == 1:
        #account_balance = (int, 'Your account balance is: $500') what are you trying to do here ??

        print('Your account balance is: ${}'.format(account_balance))
        #print('Your account balance is: $'+str(account_balance))

    elif choice == 2:
        print("Withdraw Cash, must be multiple of 10")

        while True:
            withdraw = int(input('How much do you want to Withdraw?:'))

            # 40%10 == 0 True but 42%10 == 0 False 
            if withdraw % 10 == 0:
                if withdraw > account_balance:
                    print("Insufficient funds, your account balance is",account_balance)

                elif withdraw <= account_balance:
                    #balance = account_balance - withdraw
                    account_balance  = account_balance - withdraw #update account_balance
                    print (' New Balance :', account_balance)

                    break
            else:
                print("Withdraw Cash, must be multiple of 10")


    elif choice == 3:
        print("Make Deposit")
        deposit = int(input('How much do you want to Deposit?:'))

        # as pointed out in comments by PM 2 Rings and others
        choice = input("Are you sure you want to deposit "+ str(deposit)+ "? [Y/N]")
        if choice.lower() == 'y':
            #balance = account_balance + deposit
            account_balance = account_balance + deposit
            print (' New Balance:', account_balance)

        elif choice.lower() == 'n':

            print('money not deposited')

            '''
            deposit2 = input("Enter the amount you want to deposit")
            balance = account_balance + deposit2
            print (' New Balance:', balance)
            '''

    elif choice == 4:
        print("Exit, Goodbye!")

        # no need to do both either use break or set loop to False
        #break
        loop = False # This will make the while loop to end as not value of loop is set to False
    else:
        # Any integer inputs other than values 1-5 we print an error message
        dump = input("Wrong option selection. Enter any key to try again..")

Welcome to a Virtual Credit Union ATM!
Please input your 4-digit PIN: 9876
Correct PIN, will continue to menu
Onto Menu options
1. View Account Balance
2. Withdraw cash
3. Make a deposit
4. Exit
Enter your choice [1-4]: 1
Your account balance is: $500
1. View Account Balance
2. Withdraw cash
3. Make a deposit
4. Exit
Enter your choice [1-4]: 2
Withdraw Cash, must be multiple of 10
How much do you want to Withdraw?:77
Withdraw Cash, must be multiple of 10
How much do you want to Withdraw?:400
 New Balance : 100
1. View Account Balance
2. Withdraw cash
3. Make a deposit
4. Exit
Enter your choice [1-4]: 3
Make Deposit
How much do you want to Deposit?:338
Are you sure you want to deposit 338? [Y/N]y
 New Balance: 438
1. View Account Balance
2. Withdraw cash
3. Make a deposit
4. Exit
Enter your choice [1-4]: 9
Wrong option selection. Enter any key to try again..
1. View Account Balance
2. Withdraw cash
3. Make a deposit
4. Exit
Enter your choice [1-4]: 1
Your account balance is: $438
1. View Account Balance
2. Withdraw cash
3. Make a deposit
4. Exit
Enter your choice [1-4]: 4
Exit, Goodbye!
  相关解决方案