python在计算结果中添加一个负号

时间:2012-02-16 23:30:53

标签: python

首先,我会警告你,我是新人,所以请耐心等待。我创建了以下程序(当然是为了一个类),除了当值不应为负数时,提取计算吐出负数,一切都有效。你们能看到我做错了吗?

提前致谢

#define the main
def main():
    name=input('Enter the customer\'s name: ')
    account_id=input('Enter the account ID: ')
    code=input('Enter the transaction code:')
    previous_balance=float(input('Enter the previous balance: '))
    transaction_amount=float(input('Enter the transaction amount: '))

    if code == "w" or code == "W":
        process_withdrawal (transaction_amount, previous_balance)
    else:
        if code == "d" or code == "D":
            process_deposit (transaction_amount, previous_balance)
        else:
            process_invalid_transaction_code (previous_balance)

#define process withdrawal
def process_withdrawal (previous_balance, transaction_amount):
    if previous_balance >= transaction_amount:
        print('You have entered an invalid transaction amount')
        balance=previous_balance
        print_balance (balance)                

    else:
        balance=previous_balance-transaction_amount
        print_balance (balance)        

#define process deposit
def process_deposit (previous_balance, transaction_amount):
    balance=previous_balance+transaction_amount
    print_balance (balance)

#define invalid transaction code
def process_invalid_transaction_code (previous_balance):
    print('You have entered an invalid transaction code.')
    balance=previous_balance
    print_balance (balance)

#define print balance
def print_balance(balance):
    print('Your current balance is :', format(balance, '.2f'))

main()

3 个答案:

答案 0 :(得分:3)

您对process_withdrawal的调用的第一个参数为transaction_amount,第二个参数为previous_balance,但函数声明的第一个参数为previous_balancetransaction_amount作为第二个。

试试这个:

if code == "w" or code == "W":
  process_withdrawal(previous_balance, transaction_amount)

答案 1 :(得分:1)

您正在向后传递参数。

process_withdrawal(transaction_amount,previous_balance)

def process_withdrawal(previous_balance,transaction_amount):

答案 2 :(得分:0)

我认为process withdrawl中的if语句应为

if previous_balance < transaction_amount: