简体   繁体   English

编码和python的新功能。 试图强制类型(int)为浮点答案

[英]new to coding and python. trying to force type (int) for a float answer

balance = 50 - 0.12 * 15
tracks_left = balance / 0.12
round (tracks_left, 0)
print 'you have', tracks_left,"to download"

The answer is you have 401.666666667 to download I've tried round(tracks_left, 0) and int (tracks_left) . 答案是you have 401.666666667 to download我尝试过round(tracks_left, 0)int (tracks_left) What am I doing wrong? 我究竟做错了什么?

round(tracks_left, 0) does not mutate tracks_left, but instead returns a new value. round(tracks_left, 0)不会突变tracks_left,而是返回一个新值。 Likewise with int(tracks_left) . 同样与int(tracks_left)

Try: 尝试:

tracks_left = round(tracks_left, 0)

Or: 要么:

tracks_left = int(tracks_left)

Note: int always rounds down. 注意: int总是四舍五入。

round(tracks_left, 0) doesn't change tracks_left . round(tracks_left, 0)不会更改tracks_left It just returns the value. 它只是返回值。 You have to assign it to something. 您必须将其分配给某些东西。

>>> balance = 50 - 0.12 * 15
>>> tracks_left = balance / 0.12
>>> tracks_left_rounded = round(tracks_left, 0)
>>> print('You have', tracks_left_rounded)
You have 402.0

You could as well do this: 您也可以这样做:

>>> balance = 50 - 0.12 * 15
>>> tracks_left = round(balance / 0.12)

You can also try int : 您也可以尝试int

>>> tracks_left_rounded = int(tracks_left)
>>> print('You have', tracks_left_rounded)
You have 401

I hope it helps! 希望对您有所帮助!

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 python,numpy:强制将新数组设置为float类型 - python, numpy: force new arrays to be as type float 试图构建一个简单的琐事类型游戏。 Python的新手。 - Trying to build a simple trivia type game. New to Python. 蟒蛇。 字符串编码 - Python. The coding of a string 在 Python 中将 float 转换为 int 结果错误答案 - Cast float to int in Python results wrong answer *编码/python 新手* 我正在尝试创建一个有 Y/N 答案的对话 - *new to coding/python* I am trying to create a conversation that has a Y/N answer 我正在尝试从 python 中的 dataframe 中删除 cloumn(“TYPE”)的特定行(MB,DT)。 谁能提供答案 - I am trying to delete the specific rows(MB, DT) of a cloumn(“TYPE”) from a dataframe in python. Can anyone provide the answer 尝试使用 Python 中的类来打印变量的值。 但是会弹出一个错误错误会弹出“str”和“int”的不支持的操作数类型 - Trying to print a value of a variable by using classes in Python. But a error pops up An error pops up unsupported operand type(s) for 'str' and 'int' 'int' 类型的参数在 python 中不可迭代。 请解释 - Argument of type 'int' is not iterable in python. please explain 编程新手,学习python。 试图让这个程序工作 - New to programming, learning python. Trying to get this program to work (Python) Monkeypatch __new__ 用于 python 中的 int、float、str、list、dict、set 和 module 类型的对象 - (Python) Monkeypatch __new__ for objects of type int, float, str, list, dict, set, and module in python
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM