triple and square functions don't display correctly
Question:
I was asked to write two functions – triple
and square
– the first should multiply its parameter by three and the latter raises its parameter to the power of two. At first, a "for loop" should iterate through values 1 to 10, and for each value prints its triple
and its square
. After that I need to modify this "for loop" so that it stops iteration when the square
of a value is larger than the triple
of the value, without printing anything in the last iteration. The test cases check that both functions triple
and square
are called exactly once per iteration.
I wrote a code, but I suspect it doesn’t satisfy the given condition, because it crashed the test cases:
def triple(a):
tr = a*3
return tr
def square(a):
sq = a**2
return sq
for i in range(1,11):
t1 = triple(i)
s1 = square(i)
#print(f"triple({i})=={triple(i)} square({i})=={square(i)}")
if s1 > t1:
break
else:
print(f"triple({i})=={triple(i)} square({i})=={square(i)}")
How could I improve my code?
Answers:
Since your tests check that you only call triple and square once per iteration, you have to change your print statement to use t1 and s1. So it’d look like:
print(f"triple({i}) == {t1} square({i}) == {s1}")
The way you have it now, you’re calling the functions again while printing.
I am not sure, as I am new to Python, but in your formatted string, you should try to use the variables t1 and s1 instead of calling the function so that it uses the squared value of I on the iterations of the for loop, just a thought. Sorry if this did not help!
def triple(num):
a = num*3
return a
def square(num):
b = num**2
return b
def main():
for i in range(1, 11):
trip = triple(i)
sqre = square(i)
if sqre>trip:
break
else:
print(f"triple({i})=={trip} square({i})=={sqre}")
if name == "main":
main()
I was asked to write two functions – triple
and square
– the first should multiply its parameter by three and the latter raises its parameter to the power of two. At first, a "for loop" should iterate through values 1 to 10, and for each value prints its triple
and its square
. After that I need to modify this "for loop" so that it stops iteration when the square
of a value is larger than the triple
of the value, without printing anything in the last iteration. The test cases check that both functions triple
and square
are called exactly once per iteration.
I wrote a code, but I suspect it doesn’t satisfy the given condition, because it crashed the test cases:
def triple(a):
tr = a*3
return tr
def square(a):
sq = a**2
return sq
for i in range(1,11):
t1 = triple(i)
s1 = square(i)
#print(f"triple({i})=={triple(i)} square({i})=={square(i)}")
if s1 > t1:
break
else:
print(f"triple({i})=={triple(i)} square({i})=={square(i)}")
How could I improve my code?
Since your tests check that you only call triple and square once per iteration, you have to change your print statement to use t1 and s1. So it’d look like:
print(f"triple({i}) == {t1} square({i}) == {s1}")
The way you have it now, you’re calling the functions again while printing.
I am not sure, as I am new to Python, but in your formatted string, you should try to use the variables t1 and s1 instead of calling the function so that it uses the squared value of I on the iterations of the for loop, just a thought. Sorry if this did not help!
def triple(num):
a = num*3
return a
def square(num):
b = num**2
return b
def main():
for i in range(1, 11):
trip = triple(i)
sqre = square(i)
if sqre>trip:
break
else:
print(f"triple({i})=={trip} square({i})=={sqre}")
if name == "main":
main()