gpt4 book ai didi

python-3.x - 如何使用一个变量中定义的变量并将其用作另一个变量中的参数?

转载 作者:行者123 更新时间:2023-12-03 08:41:04 24 4
gpt4 key购买 nike

我想在第一个函数中调用使用total的值,并在其他两个函数中将其用作参数。但这似乎不起作用,因为它会产生名称错误。

def all_total():
'''Calculates and displays the total including
country and s6ales tax'''
total = float(input('Enter the subtotal of the purchase: '))
county_tax(total)
print('County Tax Amount: $'+county_tax())
state_tax(total)
print('State Tax Amount: $'+state_tax())
total += (county_amount + state_amount)

def county_tax(arg1):
'''Calculates the county tax of the total'''
county_tax = float(input('Enter the county tax in percent form: '))
county_amount = county_tax/100*total
return county_amount

def state_tax(arg1):
'''Calculates the state tax of the total'''
state_tax = float(input('Enter the state tax in percent form: '))
state_amount = state_tax/100*total
return state_amount

all_total()



Enter the subtotal of the purchase: 200
Enter the county tax in percent form: .025
Traceback (most recent call last):
File "E:/Bronx Science/Sophomore/Computer Science_Python/Edwin Chen_Lab 9_Sales Tax.py", line 29, in <module>
all_total()
File "E:/Bronx Science/Sophomore/Computer Science_Python/Edwin Chen_Lab 9_Sales Tax.py", line 11, in all_total
county_tax(total)
File "E:/Bronx Science/Sophomore/Computer Science_Python/Edwin Chen_Lab 9_Sales Tax.py", line 20, in county_tax
county_amount = county_tax/100*total
NameError: name 'total' is not defined

最佳答案

尝试这个

def all_total():
'''Calculates and displays the total including
country and s6ales tax'''
total = float(input('Enter the subtotal of the purchase: '))
county_amount = county_tax(total)
print('County Tax Amount: ${}'.format(county_amount))
state_amount = state_tax(total)
print('State Tax Amount: ${}'.format(state_amount))
total += county_amount + state_amount

def county_tax(arg1):
'''Calculates the county tax of the total'''
county_tax = float(input('Enter the county tax in percent form: '))
county_amount = county_tax/100*arg1
return county_amount

def state_tax(arg1):
'''Calculates the state tax of the total'''
state_tax = float(input('Enter the state tax in percent form: '))
state_amount = state_tax/100*arg1
return state_amount

all_total()

关于python-3.x - 如何使用一个变量中定义的变量并将其用作另一个变量中的参数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61481822/

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