我有一个作业,无法找出真正的解决方案。
def triple(n): #multiplies number with 3
return (n*3)
def square(n):
return (n**2) #takes second power of number
for i in range(1,11): #I asked to iterate 1 to 10
triple_ = triple(i)
square_ = square(i)
if triple_ <= square_: #it shouldn't print if square of the number is larger than the triple
break #it must END the loop
print(f"triple({i})=={triple(i)} square({i})=={square(i)}")
triple(1)==3 square(1)==1
triple(2)==6 square(2)==4
我的代码停止打印了,但是他们不想要那样。他们想完全完成它。
edit1我对自己的陈述不实,对不起,它不应多次调用函数。他们每个人只有一次,我为此尝试过休息。
根据您的评论,如果条件全部错误:
def triple(n): #multiplies number with 3
return (n*3)
def square(n):
return (n**2) #takes second power of number
for i in range(1,11): #I asked to iterate 1 to 10
triple_ = triple(i)
square_ = square(i)
if triple_ > square_: #it should only print if the square of the number is smaller than the triple
print(f"triple({i})=={triple(i)} square({i})=={square(i)}")
中断将退出foor循环,您想要避免打印,这是一个完全不同的主题
尝试以下代码,
def triple(n): #multiplies number with 3
return (n*3)
def square(n):
return (n**2) #takes second power of number
for i in range(1,11): #I asked to iterate 1 to 10
triple_ = triple(i)
square_ = square(i)
if triple_ < square_: #it shouldnt print if square of the
number is larger than the triple
pass #it must END the loop
else:
print("Triple of " + str(i) + " is " + str(triple(i)) + " and its greater than or equal to its square " + str(square(i)))
在i = 3的情况下,平方是9,三元组也是9。因此,如果将<=替换为
此后停止打印,因为不满足任何条件。对于介于(1,10)之间的数字,三元组为正方形小于或等于三元组的唯一可能数字为1,2和3。