使用整数而不是绝对值时的代码

2024-10-03 19:24:20 发布

您现在位置:Python中文网/ 问答频道 /正文

这是一个错误,其中'costofkeg'的输入是一个绝对值,回溯显示:

Traceback (most recent call last):
  File "C:\Users\Zico\Desktop\GrossSP.py", line 23, in <module>
    portioncost = (costofkeg/(kegsize/(portionsize*0.568)))
TypeError: unsupported operand type(s) for /: 'str' and 'float'

但是,当我将“costofkeg”开头的行更改为:

costofkeg = input('Please enter the cost of keg. GBP: ')

代码将完美地工作。你知道吗

下面是需要更改的代码,但我只是不知道要更改什么。你知道吗

kegsize = int(input('Please enter keg size. Litres: '))

costofkeg = input('Please enter cost of keg. GBP: ')

abv = input('Please enter Alcohol by Volume. %: ')

gp = int(input('Please enter Gross Profit Percentage. %: '))

print('These are the Portion Size options (imperial measurements), 1/3, 1/2, 2/3, 1.')
portionsize = eval(input('Please choose Portion Size: '))
print ('')
if portionsize not in ['1/3' , '1/2' , '2/3' , '1']:
    print('Thank you, find your inputs below\n')


print ('Keg Size', kegsize, 'Litres')
print ('Cost of Keg', costofkeg, 'GBP')
print ('Alcohol by Volume', abv, '%')
print ('Gross profit percentage', gp, '%\n')


portioncost = (costofkeg/(kegsize/(portionsize*0.568)))
pc = format(portioncost, '.2f')
print('Portion Cost', pc, 'GBP')

netsp = 100*(portioncost/(100-gp))
nsp = format(netsp, '.2f')
print ('Net Selling Price', nsp, 'GBP')

grosssp = (netsp*1.2)
gsp = format(grosssp, '.2f')
print ('Gross Selling Price', gsp, 'GBP')

Tags: ofinputsizegpprintenterpleasegbp
3条回答

我有一个问题,理解你问什么,所以我会写这个一般条款。你知道吗

如果要对至少有一个数字(float或int)的表达式求值,则所有值都必须是一个数字。你知道吗

所以呢

test = input("Enter a number")
print(test/10)

将引发错误,因为test是字符串。你知道吗

要将内容转换为数字数据类型,可以执行以下操作:

testNum = input("Enter a number that will be converted to a string")
testNum = eval(testNum)

或者

testNum = float(input("Input a monetary value"))

对某些人来说,如果表达式中的一个项是float或string,那么它们都必须是float或string。检查以确保每个值都是数字数据类型。而且,你不能分割字符串!

所以就围起来

costofkeg = input('Please enter cost of keg. GBP: ')

float()

由于您希望使用不受支持的字符串进行除法(正如回溯告诉您的那样),因此引发了错误。你知道吗

所以要考虑的是:

portioncost = (costofkeg/(kegsize/(portionsize*0.568)))

如果你看一下变量的定义,你会发现第一个不是数字的变量(一个int或一个float)是costofkeg。你知道吗

因此,您应该将用户的输入转换为合适的数据类型。因为这是一个很明显的价格,所以我建议使用float。所以改变吧

costofkeg = input('Please enter cost of keg. GBP: ')

变成这样

costofkeg = float(input('Please enter cost of keg. GBP: '))

此外,您应该考虑在转换数据类型时捕获错误。那么,如果用户输入一个文本字符串作为价格,而您将其传递给float(),会发生什么呢?为了考虑到这一点,我建议使用^{}块来处理错误。如果对每个输入使用多个函数来整理代码,那么在将输入转换为所需数据类型时发生错误时,可以从这些函数本身调用这些函数。你知道吗

这看起来像这样(只是一个使用您已有代码的简短示例片段):

def ask_int(string):
    try:
        val = int(input(string))
    except ValueError as e:
        print('Invalid format. Your input was not an integer.')
        ask_int(string)
    else:
        return val

def ask_float(string):
    try:
        val = float(input(string))
    except ValueError as e:
        print('Invalid format. Your input was not a float')
        ask_int(string)
    else:
        return val

kegsize = ask_int('Please enter keg size. Litres: ')
costofkeg = ask_float('Please enter cost of keg. GBP: ')
abv = ask_float('Please enter Alcohol by Volume. %: ')
gp = ask_int('Please enter Gross Profit Percentage. %: ')

portionsize的输入可能成为另一个问题。将其作为一个函数编写可能如下所示:

def ask_portionsize(string):
    val = ask_float(string)
    if val not in [1/3 , 1/2 , 2/3 , 1]:
        print('Thank you, find your inputs below\n')
    else:
        try:
            val = float(val)
        except ValueError as e:
            ask_portionsize()
        else:
            return val

portionsize = ask_portionsize('Please choose Portion Size: ')

要使代码同时处理整数和绝对值,请将这些更改应用于代码。你知道吗

kegsize = int(input('Please enter keg size. Litres: '))

costofkeg = float(input('Please enter cost of keg. GBP: '))

abv = input('Please enter Alcohol by Volume. %: ')

gp = int(input('Please enter Gross Profit Percentage. %: '))

print('These are the Portion Size options (imperial measurements), 1/3, 1/2,     2/3, 1.')
portionsize = eval(input('Please choose Portion Size: '))
print ('')
if portionsize not in ['1/3' , '1/2' , '2/3' , '1']:
    print('Thank you, find your inputs below\n')


print ('Keg Size', kegsize, 'Litres')
print ('Cost of Keg', costofkeg, 'GBP')
print ('Alcohol by Volume', abv, '%')
print ('Gross profit percentage', gp, '%\n')

注意'(portionsize'前面的float,这是因为你不能分割字符串,所以我不得不浮动变量。你知道吗

portioncost = (costofkeg/(kegsize/float(portionsize*0.568)))
pc = format(portioncost, '.2f')
print('Portion Cost', pc, 'GBP')

netsp = 100*(portioncost/(100-gp))
nsp = format(netsp, '.2f')
print ('Net Selling Price', nsp, 'GBP')

grosssp = (netsp*1.2)
gsp = format(grosssp, '.2f')
print ('Gross Selling Price', gsp, 'GBP')

相关问题 更多 >