gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-28 23:02:52 24 4
gpt4 key购买 nike

首先,我会警告你,我是新手,所以请耐心等待。我创建了以下程序(当然是为了一个类(class))并且一切正常,除了取款计算吐出一个负数,而该值不应为负数。你们能看出我做错了什么吗?

提前致谢

#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()

最佳答案

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

试试这个:

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

关于python在计算结果中添加一个负号,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9320736/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com