-2

I have an assignment and couldn't figure out the true solution.

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):
    if triple(i) > square(i):
        print((f"triple({i})=={triple(i)} square({i})=={square(i)}"))
triple(1)==3 square(1)==1
triple(2)==6 square(2)==4
  1. I should stop the iteration when the square of a value is larger than the triple of the value, without printing anything in the last iteration.

  2. And both functions triple and square are must called exactly once per iteration.

other things i tried

    ls =[f"triple({i})=={triple(i)} square({i})=={square(i)}" for i in range(1,11) if triple(i) > square(i)]
    for i in ls:
        print(i)

There is a test that checks my answers and it says "wrong number of printed lines", I asked someone from course they just told me that ı should store the values getting from each functions into a variable. And these are what I tried to do what they said

  • Yes, because triple(3) is 9 and square(3) is 9 and on that condition you are breaking. If you want the program to continue, then don't break, simple. – Harshal Parekh Jun 03 '20 at 19:20

2 Answers2

1

According to your comments, you have the if condition all wrong:

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)}")

break will exit the foor loop, you instead want to avoid printing and that is a completely different subject

Daemon Painter
  • 3,208
  • 3
  • 29
  • 44
0

Try the following code,

    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)))

In case of i = 3, the square is 9 and triple is also 9. So if you replace the <= to < you will also get the output for i = 3. After that, it will not print the result as the condition will not met.

It stops printing after that because none of the conditions is met. For numbers between (1,10), the only possible numbers for which triple is square is less than or equal to triple is 1,2 and 3.