python代码中的数学问题。公式问题
这是我写的代码,请在最后一行下面找到回溯python代码中的数学问题。公式问题,python,python-3.x,Python,Python 3.x,这是我写的代码,请在最后一行下面找到回溯 kegsize = int(input('Please enter keg size. Litres: ')) costofkeg = int(input('Please enter cost of keg. GBP: ')) abv = input('Please enter Alcohol by Volume. %: ') gp = int(input('Please enter Gross Profit Percentage. %: '))
kegsize = int(input('Please enter keg size. Litres: '))
costofkeg = int(input('Please enter cost of keg. GBP: '))
abv = input('Please enter Alcohol by Volume. %: ')
gp = int(input('Please enter Gross Profit Percentage. %: '))
opt = print('These are the Portion Size options (imperial measurements), 1/3, 1/2, 2/3, 1')
portionsize = input('Please choose Portion Size: ')
if portionsize not in ['1/3' , '1/2' , '2/3' , '1']:
print('Please enter a Portion Size from the list')
else:
print('Thank you')
print ('Keg Size', kegsize, 'Litres')
print ('Cost of Keg', costofkeg, 'GBP')
print ('Alcohol by Volume', abv, '%')
print ('Gross profit percentage', gp, '%')
GrossSp = (costofkeg/(kegsize/portionsize*0.568)/(1-gp))*1.2)
我似乎在过去也遇到过类似的错误,但是我找不到任何输出相同回溯的代码。请有人教我如何改正这些错误
Traceback (most recent call last):
File "C:/Users/Zico/Desktop/Papa, code.py", line 23, in <module>
GrossSp = (costofkeg/(kegsize/portionsize*0.568)/(1-gp))*1.2
TypeError: unsupported operand type(s) for /: 'int' and 'str'
回溯(最近一次呼叫最后一次):
文件“C:/Users/Zico/Desktop/Papa,code.py”,第23行,在
Grossp=(桶的成本/(桶大小/比例大小*0.568)/(1-gp))*1.2
TypeError:/:“int”和“str”的操作数类型不受支持
您正在尝试按字符串(portionsize)进行除法 您应该使用eval(输入(…)
将其设置为
int
将不起作用,因为它可以是1/3、1/2或2/3,而这不是int
,因此这样做会破坏程序。portionsize
是str,而不是数值。当您检查该值是否在允许值列表中时,不会最终将其转换为数字
您可以单独测试每个选项,也可以使用dict:
try:
portionsize = {'1/3': 0.33, '1/2': 0.5, '2/3': 0.67, '1': 1}[portionsize]
except KeyError:
print('Please enter a Portion Size from the list')
portionsize
是一个字符串…您需要像对其他int(input())
一样转换它…抱歉,我不懂。如果可以的话,你能帮我做一下吗。非常感谢。您的问题是行portionsize=input('请选择部分大小:')
,如果发生这种情况,它需要是int或float。回溯(最近一次调用):文件“C:/Users/Zico/Desktop/Papa,code.py”,第11行,在portionsize=float(输入('请选择部分大小:'))值错误:无法将字符串转换为float:'1/3'如果发生这种情况。回溯(最近一次调用):文件“C:/Users/Zico/Desktop/Papa,code.py”,第11行,在portionsize=float(输入('请选择部分大小:'))值错误:无法将字符串转换为float:'1/3'@MotoStromm这很奇怪。请改为尝试eval
。“