Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/279.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 如何得到整数?_Python_Integer - Fatal编程技术网

Python 如何得到整数?

Python 如何得到整数?,python,integer,Python,Integer,作为一名新的python程序员。我试着自己创造和解决问题。我使用Python3 代码如下: def next(): print ("how_much="), if input = 50: print("Nice, you're not greedy, you win"), else: print("You greedy bastard!") 我想将值转换成整数,如果数字是50,则打印“很好,你不贪婪,你赢了”。否则,打印“不错,你不贪婪,

作为一名新的python程序员。我试着自己创造和解决问题。我使用Python3

代码如下:

def next():
    print ("how_much="),
    if input = 50:
        print("Nice, you're not greedy, you win"),
    else:
        print("You greedy bastard!")
我想将值转换成整数,如果数字是50,则打印“很好,你不贪婪,你赢了”。否则,打印“不错,你不贪婪,你赢了”

我受启发,基于此代码创建了上述代码:

def is_int(x):
    print round(x),
    if x == round(x):
        print 'True',
    else:
        print 'False'

is_int(7.0)   # True
is_int(7.5)   # False
is_int(-1)    # True
您只需将add转换为整数,并将“=”(赋值)更改为“=”(比较)


如果要确定变量是否为int,可以执行以下操作: (在Python2.7上工作,在3上检查..)


1.使用
=
进行比较,使用
=
进行作业2。您正在跟踪函数
next
3。
True
之后有一个不必要的尾随逗号。但最重要的是,你的问题不清楚,我有点困惑。您需要使用
input
(或Python 2.7的
raw\u input
)让用户能够与您的程序交互(
print
不允许他们响应)。如果how_mound=50,您还有
,这将失败,因为您需要使用
=
进行比较。
的代码是_int()
似乎满足了您的要求。你到底在问什么,因为没有问题,只有一个声明?在最初的帖子中没有实际的问题,我不确定这是什么意思。此外,此代码有多个错误(
print(“多少:+x)
没有任何用处,因为它不会引起用户的响应)。是的,原始帖子非常模糊。但是根据OP使用的代码,看起来他不需要从用户那里获取输入——提到的函数也不接受用户的任何输入。但我明白你的意思。伙计们,我所要做的就是,如果下一个整数(50),它会打印“很好,你不贪婪,你赢了”,否则它会打印“你这个贪婪的混蛋!”我不会复制和粘贴。我从堆栈中找到的其他代码中创建了一个原始代码overflow@PravinNath我编辑了我的答案,现在应该可以做你想要的了。还请编辑初始问题,使其不含糊。
def next_int(x)
    input = int(x) # convert string to integer
    if input == 50:
        print("Nice, you're not greedy, you win")
    else:
        print("You greedy bastard!")
def is_int(x):
     return type(x) == int