Python 使用整型值而非绝对值时的代码

Python 使用整型值而非绝对值时的代码,python,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'

这是一个错误,“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'
代码将完美地工作

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

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')

我在理解你的问题时遇到了一个问题,所以我将用一般术语来写这篇文章

如果要计算至少有一个数字(float或int)的表达式,则所有内容都必须是数字

所以

将引发错误,因为
test
是字符串

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

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

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

所以就围绕着,

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

使用
float()

会引发错误,因为您希望使用不受支持的字符串进行除法(正如回溯告诉您的那样)

所以要考虑的是:

portioncost = (costofkeg/(kegsize/(portionsize*0.568)))
如果你看一下变量的定义,你会发现第一个变量不是一个数字(一个
int
或一个
float
)是
costofkeg

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

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

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

此外,在转换数据类型时,应考虑捕获错误。那么,如果用户输入一个文本字符串作为价格,然后将其传递给

float()
,会发生什么情况呢?为了考虑到这一点,我建议使用块来处理错误。如果您为每个输入使用多个函数来整理代码,那么如果在将输入转换为所需数据类型时发生错误,您可以自己调用这些函数

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

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')

你能告诉我什么是正确的,什么是错误的版本吗?没有正确的版本。这是因为当我尝试使用整数时,代码会工作,但只有在一定程度上,我这样说,因为当我尝试任何不是整数的东西时,我会得到上面显示的回溯。我会怎么做ke表示无论输入的数字是整数还是带小数的数字,代码都可以工作。提前谢谢。那么我如何将“portionsize”变量更改为此行中的字符串。portioncost=(costofkeg/(kegsize/(portionsize*0.568)))为什么要将其更改为字符串?您不能拆分字符串。您可以通过
str(portionsize)
执行此操作,非常有意义,谢谢。请参阅下面我的答案。:)
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. %: ')
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')
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')