因此,正如标题所暗示的那样,这是该书中的一个示例。我对编程仍然很陌生,并且难以调试。话虽如此,欢迎任何批评,特别是如果它显示了一种更有效的编码方式;请记住,我还是新手,所以如果你向我扔一个新的内置函数或其他东西,我很可能不知道你指的是什么。
所以这个练习的重点是编写一个函数,给它三个参数,以确定这三个参数是否形成一个三角形。这是我的代码:
def is_triangle(a,b,c):
num_list = [a,b,c]
biggest = max(num_list)
other_two = num_list.remove(biggest)
sum_of_two = sum(other_two)
if sum_of_two > biggest:
print 'Congrats, %d, %d, and %d form a triangle!' % (a,b,c)
elif sum_of_two == biggest:
print 'That forms a degenerate triangle!'
else:
print 'That does\'t make any sort triangle... >:['
def sides():
side1 = raw_input('Please input side numero Juan: ')
side2 = raw_input('Now side two: ')
side3 = raw_input('...aaaannnd three: ')
import time
time.sleep(1)
print 'Thanks >:]'
side1 = int(side1)
side2 = int(side2)
side3 = int(side3)
is_triangle(side1,side2,side3)
sides()
但是,每当我运行它时,我都会得到以下信息:
Traceback (most recent call last):
File "A:/Python/is_triangle.py", line 27, in <module>
sides()
File "A:/Python/is_triangle.py", line 25, in sides
is_triangle(side1,side2,side3)
File "A:/Python/is_triangle.py", line 5, in is_triangle
sum_of_two = sum(other_two)
TypeError: 'NoneType' object is not iterable
我的猜测是 sum_of_two 行,但我不知道它有什么问题。有人可以帮我调试吗?
我花了一个小时在没有内置函数的情况下重写它(以各种方式,or
到处都是一堆 s)。但它看起来很糟糕,我宁愿学着这样写。