相关疑难解决方法(0)

如何将输入读作数字?

在下面的代码中为什么xy字符串而不是整数?网上的所有内容都说要使用raw_input(),但是我读input()raw_input()在Python 3.x中重命名的Stack Overflow(在一个不处理整数输入的线程上).

play = True

while play:

    x = input("Enter a number: ")
    y = input("Enter a number: ")

    print(x + y)
    print(x - y)
    print(x * y)
    print(x / y)
    print(x % y)

    if input("Play again? ") == "no":
        play = False
Run Code Online (Sandbox Code Playgroud)

python int input python-2.7 python-3.x

261
推荐指数
7
解决办法
76万
查看次数

Python 2如何比较字符串和int?为什么列表比较大于数字,而元组大于列表?

以下代码段使用输出进行注释(如ideone.com上所示):

print "100" < "2"      # True
print "5" > "9"        # False

print "100" < 2        # False
print 100 < "2"        # True

print 5 > "9"          # False
print "5" > 9          # True

print [] > float('inf') # True
print () > []          # True
Run Code Online (Sandbox Code Playgroud)

有人可以解释为什么输出是这样的?


实施细节

  • 这种行为是由语言规范强制执行的,还是由实现者决定的?
  • 任何主要的Python实现之间是否存在差异?
  • Python语言版本之间是否存在差异?

python comparison types python-2.x

172
推荐指数
2
解决办法
8万
查看次数

标签 统计

python ×2

comparison ×1

input ×1

int ×1

python-2.7 ×1

python-2.x ×1

python-3.x ×1

types ×1