# Numbers and Squares

Testing Number and Squares, my current solution is printing is 13 true’s or 13 false’s, I want it to print just one true or one false depending on the condition set. https://code.sololearn.com/c1evg4DuxVKW/?ref=app

5/9/2020 8:37:05 PM

Dr. Exceptional18 Answers

New AnswerDr. Exceptional numbers = (1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13) squares = (1, 4, 9, 16, 25, 36, 49, 64, 81, 100, 121, 144, 169) result = all(numbers[i] ** 2 == squares[i] for i in range(len(numbers))) print(result) you don't need any loop nor conditional since all() does that for you.

You shudn't use for loop then (numbers[i] ** 2 == squares[i]for i in range(len(numbers))) as this is already checking all 13 numbers and printing a tuple of True or False or maybe I am not really understanding what you are doing!

In this statement (numbers[i]**2==squares[i] for i in range(Len(numbers))) list length is 13 here ,so first number[0]**2==squares[0] is checked for i=0 ,which is True so True is stored in the list at 0th position ,now number[1]**2==square[1] is checked for i=1,which again is true ,so this goes on and on and when i=13 it stops ,so now you have a tuple with 13 true's so applying All will result in true as all are true and that for i in number is just repeating this again and again 13 times ,that i has nothing to do with i in that list comprehension

I think Abhay has done a good job explaining this. Or what exactly are you trying to do with the code?

As Abhay wrote, the all() function already supplies the complete solution for your problem, as I understand it... What you are doing additionally is this for loop, which actually just repeats the solution to the problem 13 times, so it is obsolete.. The if statement will print True, if all() returns True, and print False, if all() returns False... So you could just print all(), and save some lines:) numbers = (1,2,3,4,5,6,7,8,9,10,11,12,13) squares = (1,4,9,16,25,36,49,64,81,100,121,144,169) print(all (numbers[i] ** 2 == squares[i]for i in range(len(numbers)))) #if output must be lower case, as in your example, convert to string and convert to lower case print(str(all (numbers[i] ** 2 == squares[i]for i in range(len(numbers)))).lower())

Dr. Exceptional you can use a jump statement to get out of the for loop that's break for i in x: if all (x[i] ** 2 == y[i] for i in range(len(x))): print("true") break else: print("false") break

𝐊𝐢𝐢𝐛𝐨 𝐆𝐡𝐚𝐲𝐚𝐥 , using break will only run once and stop, if I have incorrect values in the tuple, it will print true instead of false, like, 169 replaced with 16 in y.

Dr. Exceptional the if all(statement) makes sure it only appends true 13 times or false 13 times, so there can only be all trues or false. Changing any value still gives the correct output

#Got the Idea from Calvin Capstone numbers = (1,2,3,4,5,6,7,8,9,10,11,12,13) squares = (1,4,9,16,25,36,49,64,81,100,121,144,169) if all (numbers[i] ** 2 ==squares[i]for i in range(len(numbers))): print("true") else: print("false")

G B Your solution still printing 13 True’s or 13 False’s, want it to be just one True or one False. If only one is interation is false, all will be false and can only be true when all are true.

Justus It will print true or false because you created an empty list and append the boolean and print out the first position, what if I change the 5th element in squares from 25 to 26, your answer will still be true and wrong because it would not catch 26 because your list is printed with the 1st position only

Abhay According to your solution, all 13 true’s will print true which is accurant, what if I have 12 true’s and 1 False, what will the output be.