将输入转换为字符串为整数

时间:2016-04-24 22:01:31

标签: python-3.x integer

我正在编写一些代码,通过文本提示(命令行)接受用户输入。此代码的目标是获取用户输入,确保它满足某些要求,然后使用它来编辑其他变量。这是代码,(略微改变了输入和变量的名称:)

import os

command = 0
cmd = input(">")
command = int(cmd)
if command <= 0:
    print("That number is too low.")
    os.exit(1)
elif command >= 11:
    print("That number is too high.")
    os.exit(1)
else:
example1 = command
example2 = command + example1

然而,当它运行时它声称有unorderable types: str() <= int()。 由于此代码不起作用,有人可以帮助解决它或告诉我另一种将输入转换为整数的方法吗?我正在使用Python 3 +。

1 个答案:

答案 0 :(得分:0)

import os

command = int(input(">"))

if command <= 0:
    print("That number is too low.")
    os.exit(1)
elif command >= 11:
    print("That number is too high.")
    os.exit(1)
else:
    example1 = command
    example2 = command + example1
相关问题