【问题标题】:Traceback error: TypeError float object cannot be interpreted as an integer回溯错误:TypeError 浮点对象不能解释为整数
【发布时间】:2015-04-01 04:03:22
【问题描述】:

有人可以帮我弄清楚我遇到的问题吗?

def marbles():
    marbles = 0
    while True:
        try:
            x = eval(input("How many marbles? "))
        except ValueError: 
            print("You can't enter", x , "marbles! How many marbles do you have?")
            continue
        else:
            break
    for i in range(x):
        x = eval(input("Please enter how many marbles between 0 and 100: "))
        if 0 <= x and x <= 100:
            marble = marble + x
        else:
            print("Your number is out of range!")
            y = int(input("Please enter how many marbles between 0 and 100: "))

main()

我似乎无法弄清楚为什么当我编写 5.4 弹珠时它不会发出 You are not in range 的警告。在 0 到 100 之间,应该允许我给出小数,但是对于“有多少弹珠”,我希望收到该警告以重试。

【问题讨论】:

  • 你为什么在某些地方使用eval(input(...))(不好!)而在其他地方使用int(input(..))?请注意,如果您想评估 literals,您应该真正使用 ast 模块的 literal_eval。此函数类似于 eval,但不解释 任意 代码,而只解释 python 文字,使其可以安全地用于不受信任的输入。

标签: python floating-point integer typeerror


【解决方案1】:

你需要字符串的isdigit 方法。 像这样?

def marbles():
    marbles = 0
    count_flag = False
    while count_flag is False:
        try:
            x = raw_input("How many marbles? ")
            if not x.isdigit():
                raise ValueError
        except ValueError:
            print "You can't enter %s marbles! How many marbles do you have?" % (x)
        else:
            x = int(x)
            count_flag = True
    for i in range(x):
        x = int(input("Please enter how many marbles between 0 and 100: "))
        if 0 <= x and x <= 100:
            marbles = marbles + x
        else:
            print("Your number is out of range!")
            y = int(input("Please enter how many marbles between 0 and 100: "))

    return marbles

print marbles()

此外,对于 python,您可以执行 0

你的 for 语句逻辑也是一个缺陷。如果用户给出了两个错误的输入,则不会考虑它们。你也需要一段时间。

while x > 0:
   y = int(input("Please enter how many marbles between 0 and 100: "))
   if 0 <= y and y <= 100:
       marbles = marbles + y
       x -= 1
   else:
       print("Your number is out of range!")

说实话,更简洁的做法是将输入验证放在另一个函数中,然后在弹珠函数中调用它。

def get_number(screen_input):
    flag = False
    while flag is False:
        try:
            x = raw_input(screen_input)
            if not x.isdigit():
                raise ValueEror
        except ValueError:
            print("You can't enter %s marbles! How many marbles do you have?" % (x))
        else:
            return int(x)

def marbles():
    marbles = 0
    x = get_number("How many marbles?")
    while x > 0:
        y = get_number("Please enter how many marbles between 0 and 100:")
        if 0 <= y <= 100:
            marbles += y
            x -= 1
        else:
            print("Your number is out of range!")
    return marbles

print marbles()

【讨论】:

    【解决方案2】:

    使用is_integer() 方法。如果参数是否为整数,则返回布尔值。

    例如

    >>> (5.4).is_integer()
    False
    >>> (1).is_integer()
    True
    

    查看this documentation.

    【讨论】:

      猜你喜欢
      • 2021-12-05
      • 1970-01-01
      • 1970-01-01
      • 2017-08-01
      • 2021-07-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-12-26
      相关资源
      最近更新 更多