【问题标题】:how to make a string an integer in python如何在python中使字符串成为整数
【发布时间】:2016-07-06 06:57:04
【问题描述】:
    Distance = input("How far are you from the spacecraft in meters? (full            positive numbers) \n")
number = Distance.isdigit()
while number == False:
    print ("Please enter a number or full number")
    Distance = input("How far are you from the spacecraft in meters? (full positive numbers) \n")
    number = Distance.isdigit()
while Distance < 600:
    print ("Please move back further from the space craft! \n")
    Distance = input("How far are you from the spacecraft in meters? (full positive numbers) \n")

所以我试图将一个字符串与一个整数进行比较,但我不确定如何在不破坏这部分的情况下解决这个问题

数字 = 距离.isdigit() 而数字 == 错误:

【问题讨论】:

  • 你可以投:Distance = int(input("blabla"))
  • 尝试使用 int(input("somestringinput")) 进行强制转换,并在输入字符串为 NAN 时将其放入 try-except 块中以正确处理。

标签: string python-3.x int helper


【解决方案1】:

我觉得你可以用这个。

def is_digit(Distance):
    try:
        if int(Distance) & int(Distance) >0:
            return True
        return False
    except ValueError:
        return False

【讨论】:

    猜你喜欢
    • 2011-01-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-05-25
    • 2021-11-12
    • 2016-12-29
    • 2010-12-27
    相关资源
    最近更新 更多