Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/297.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/python-2.7/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 2中的乘法错误_Python_Python 2.7 - Fatal编程技术网

Python 2中的乘法错误

Python 2中的乘法错误,python,python-2.7,Python,Python 2.7,我无法理解Python 2.7中的乘法问题。我相信答案很简单!从我的代码的简单性可以看出,我是一个初学者,见下文 from __future__ import division goAgain = 'yes' while goAgain == 'yes': meal = raw_input('How much did your meal cost? ') tip = raw_input('Do you want to tip a set PRICE or a PERCENTA

我无法理解Python 2.7中的乘法问题。我相信答案很简单!从我的代码的简单性可以看出,我是一个初学者,见下文

from __future__ import division

goAgain = 'yes'

while goAgain == 'yes':
    meal = raw_input('How much did your meal cost? ')
    tip = raw_input('Do you want to tip a set PRICE or a PERCENTAGE of your total bill? ').upper()
    print

    if tip == 'PRICE':
        tip = raw_input('How much do you want to tip? ')
        bill = int(meal) + int(tip)
        print 'As your meal cost ' + str(meal) + ' and your tip is ' + str(tip) + ', your total bill is ' + str(bill) + '.'
    elif tip == 'PERCENTAGE':
        tip = raw_input('What percentage would you like to tip? ')
        tip = int(tip) / 100
        print 'Your tip is ' + str(tip)
        bill = (int(meal) * int(tip)) + int(meal) # This is where my problem is
        print 'The bill is ' + str(bill)
        print 'As your meal cost ' + str(meal) + ' and you tipped ' + str(tip) + ' percent, your total bill is ' + str(bill) + '.'
    else:
        tip = raw_input("Sorry, I didn't catch that! Do you want to tip a set PRICE or a PERCENTAGE of your total bill? ").upper()
我遇到的问题是,程序总是告诉我,我的账单总额与膳食变量的价格相同,尽管我可以看到我将膳食和小费值相加。

将小费除以100,得到的数字小于1,这是非常合理的。然后,当您将其相乘时,将其转换为整数。INTMEIN*inttip+INTMEIN

如果将0到1之间的数字转换为int,则得到零

相反,如果要将结果强制转换为整数,可以执行以下操作:

bill = int(int(meal)*tip) + int(meal)
或者,您可能希望尝试强制转换为浮动,而不是int。这可能会给出更合适的结果。

因为您使用了:

tip = int(tip) / 100
tip现在是一个浮动:

>>> tip = int(tip) / 100
>>> type(tip)
<type 'float'>

转换浮点值正如@khelwood已经指出的问题是,在python2中用/除以两个整数总是得到一个整数,因此有效小费率为零

问题的根源在于您使用的是Python 2。Python2中的除法是不直观的,但出于向后兼容性的原因,除法仍以这种方式工作。通过切换到Python 3,或者在脚本顶部添加以下内容,可以一次性解决此问题:

from __future__ import division

当你用/除两个数时,你总是会得到一个浮点数。还有//,它也会给你一个整数结果。

你应该用float而不是int来表示你的数字,因为int表示整数,float表示带小数的数字,你的tip值应该是除以100后的小数。在这种情况下,这不是真正的问题。tip变量除以100后,问题中的代码显式地将其转换为int。这在任何版本的Python中都是零。你说得对。。。不过,它们都是问题,都需要解决。
>>> int(0.7)
0
bill = (int(meal) * tip) + int(meal)
bill = int((int(meal) * tip) + int(meal))
from __future__ import division