2015-12-20 108 views
-3

这里是我写的代码,请在最后一行下面找到回溯。Python代码中的数学问题。公式问题

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' 
+3

'portionsize'是一个字符串...你需要将其转换就像你为别人'INT(输入() )'... –

+0

对不起,我没有关注。如果没关系,你可以帮我做吗?谢谢! – user5701168

+0

你的问题是'linesize = input('请选择部分大小:')'这需要是一个整数或浮点数 – dawg

回答

-1

你应该让portionsize使用eval(input(...))

一批使其成为一个int将无法​​正常工作,因为它可以是1/3,1/2或2/3,这是不是一个int,所以这样做将打破你的计划。

+0

如果我将这种情况浮动。 Traceback(最近调用最后一个):文件“C:/ Users/Zico/Desktop/Papa,code.py”,第11行,在 partsize = float(input('请选择部分大小:'))ValueError:could not将字符串转换为float:'1/3' – user5701168

+0

@MotoStromm这很奇怪。试试'eval'。 – wythagoras

+0

'eval'的作品,谢谢。 :) – user5701168

3

您试图按字符串(份数大小)进行分割。

4

portionsize是一个str,而不是一个数字值。当您检查该值是否在允许的值列表中时,您最终不会将其转换为数字。

你既可以检验每个选项单独,或使用字典:

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