【问题标题】:unorderable types: str() > int()不可排序的类型:str() > int()
【发布时间】:2016-02-13 03:38:55
【问题描述】:

所以我尝试制作的程序由三个输入组成,分别等于右、等腰或钝角三角形,当我运行我的代码时,我遇到了这个问题。我已经把 int() 放在了所有东西的前面。我做错了什么?

angle_1 = input("What is the degree of the first angle? ")
angle_2 = input("What is the degree of the second angle? ")
angle_3 = input("What is the degree of the third angle? ")


if int(angle_1 or angle_2 or angle_3) == 90:
    print("This is a right triangle.")
elif int((angle_1 or angle_2 or angle_3) > 90) and int((angle_1 or angle_2 or angle_3) < 180):
    print("This is an obtuse triangle.")
else:
    print("This is an acute triangle.")     

【问题讨论】:

  • 你发明了自己的语法,这与 Python 使用的解释不同。考虑int(angle_1 or angle_2 or angle_3)。这将对三个角度执行逻辑或,然后将结果转换为int。显然不是你想要的。所以,修复它。

标签: python string int


【解决方案1】:

if 语句中的语法很不可靠。我会列出角度名称,然后遍历它们。

angle_1 = input("What is the degree of the first angle? ")
angle_2 = input("What is the degree of the second angle? ")
angle_3 = input("What is the degree of the third angle? ")

# This will be looped through
angle_list=[angle_1, angle_2, angle_3] # This will be looped through

for angle in angle_list: # Goes through each inputted angle. 
    if angle == 90:
        print 'This triangle is right.'
        break
    elif angle > 90:
        print 'This triangle is obtuse.'
        break
    else:
        print 'This is an acute triangle.'
        break

为了将来参考,您可能希望在处理新概念时直接参考文档。 or 是一个逻辑运算符,而不是布尔值。

【讨论】:

  • 这是不对的。该列表包含文字字符串“angle_1”等...,而不是用户输入的值。即使这样做了,您一次只检查一个值并最终打印关于角度的三件事。尝试运行它,你会看到。
  • @tdelaney 对!对于那个很抱歉。我认为列表需要字符串本身(否则我会得到TypeError),但您对第二点是绝对正确的。我将在for 循环中添加一个break
  • @tdelaney 我解决了第一个问题——我必须将列表移动到变量声明之后。
  • 问题仍然存在。 angle_list 仍然是字符串名称,需要去掉引号才能得到变量名。下一个问题是您还没有将输入从字符串转换为 int,所以无论如何,您的代码都会认为三角形是钝角。最后,您只考虑列表中的第一项(它总是会中断),例如,如果用户输入 30、60、90,程序将看到 30 并假定它是一个锐角三角形。
  • 原始海报使用 python 3,其中 python 2 的 raw_input 现在是 3 的 input 和 python 2 的 input(将字符串评估为 python 表达式)被完全删除。我没有注意到您使用的是 python 2。很抱歉造成混淆!顺便说一句,你的课程可能需要 python 2,但你真的应该学习 3。祝你好运! (并以 30、60、90 运行您的程序 - 它仍然需要 tweeks)。
【解决方案2】:

您的第一个问题是您需要将输入从字符串转换为 int。名义上,这是由val = int(intput('something: ')) 完成的,但由于用户有输入垃圾的习惯,因此您需要在转换时捕获错误。这是一个很好的函数角色。

您的第二个问题与or 运算符有关,正如在多个地方所描述的那样。考虑到解决方案,您有几个要比较的值,因此将它们放入列表中是很自然的。完成此操作后,python 会提供一些技巧和有用的函数,让您可以就这些值提出问题。

把它们放在一起,你就得到了

def get_input(prompt, cast_to=str):
    while True:
        try:
            val = input(prompt)
            return cast_to(val)
        except ValueError:
           print("'{}' is in valid, try againr".format(val))

angle_1 = get_input("What is the degree of the first angle? ", int)
angle_2 = get_input("What is the degree of the second angle? ", int)
angle_3 = get_input("What is the degree of the thrid angle? ", int)

angles = [angle_1, angle_2, angle_3]

if sum(angles) != 180:
    print("We are sticking with Euclidean geometry pal")
elif 90 in angles:
    print("This is a right triangle.")
elif max(angles) > 90:
    print("This is an obtuse triangle.")
else:
    print("This is an acute triangle.")

【讨论】:

    猜你喜欢
    • 2013-01-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-05-08
    • 2014-08-14
    • 2016-12-18
    • 1970-01-01
    相关资源
    最近更新 更多