【问题标题】:Problem with 'less than' and 'greater than' signs in Python [duplicate]Python中“小于”和“大于”符号的问题[重复]
【发布时间】:2021-12-22 11:48:42
【问题描述】:
a = input('a= ')
b = input('b= ')
c = input('c= ')

print(c<(a+b))
print(b<(a+c))
print(a<(b+c))

print (a)
print (b)
print (c)

while not(c<(a+b) and b<(a+c) and a<(b+c)):
    print('not a triangle.')
    a = input('a= ')
    b = input('b= ')
    c = input('c= ')
    




print('Loaded triangle a= {0}, b= {1}, c= {2}.'.format(a,b,c))

这应该是一个简单的程序,用于检查 3 条边是否构成三角形。但即使是个别支票也没有字(3,4,5),一些随机组合有效(5,5,5)

【问题讨论】:

  • 您需要将输入转换为数字,例如a = float(input('a= '))

标签: python python-3.x oop


【解决方案1】:

input 函数返回一个字符串。您在这里所做的一切都是在使用字符串。 '3'+'4' 给你'34',而不是你想要的。你需要:

a = int(input('a= '))
b = int(input('b= '))
c = int(input('c= '))

在那里进行一些调试打印会更快。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2022-06-16
    • 1970-01-01
    • 2021-09-01
    • 1970-01-01
    • 2021-10-03
    • 1970-01-01
    • 1970-01-01
    • 2017-08-30
    相关资源
    最近更新 更多