+ 3

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

9th May 2020, 8:37 PM
Jibril Balogun
Jibril Balogun - avatar
18 Answers
+ 6
Dr. 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.
9th May 2020, 11:49 PM
Sebastian Pacurar
Sebastian Pacurar - avatar
+ 4
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!
9th May 2020, 8:50 PM
Abhay
Abhay - avatar
+ 3
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
9th May 2020, 9:09 PM
Abhay
Abhay - avatar
+ 2
I think Abhay has done a good job explaining this. Or what exactly are you trying to do with the code?
10th May 2020, 12:20 AM
👑 Tchybooxuur!
👑 Tchybooxuur! - avatar
+ 1
Lothar This is the code 👑 Tchybooxuur! Look at this
9th May 2020, 8:38 PM
Jibril Balogun
Jibril Balogun - avatar
+ 1
𝐊𝐢𝐢𝐛𝐨 𝐆𝐡𝐚𝐲𝐚𝐥 , 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.
9th May 2020, 8:44 PM
Jibril Balogun
Jibril Balogun - avatar
+ 1
Abhay I do not get it, explain further
9th May 2020, 8:56 PM
Jibril Balogun
Jibril Balogun - avatar
+ 1
https://code.sololearn.com/cpcC9BIVFswz/?ref=app My solution, checks all condition
9th May 2020, 9:09 PM
Justus
Justus - avatar
+ 1
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())
9th May 2020, 10:56 PM
G B
G B - avatar
+ 1
Dr. Exceptional i think you don't need that 1st loop
11th May 2020, 10:33 AM
Bhavuk Sharma
Bhavuk Sharma - avatar
0
Sebastian Pacurar , can you look at this?
9th May 2020, 11:34 PM
Jibril Balogun
Jibril Balogun - avatar
0
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
9th May 2020, 11:41 PM
Justus
Justus - avatar
0
Thank you all for your inputs, I appreciate.
10th May 2020, 1:01 AM
Jibril Balogun
Jibril Balogun - avatar
0
#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")
11th May 2020, 4:57 PM
Atharva Pethkar
Atharva Pethkar - avatar
- 1
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.
9th May 2020, 11:30 PM
Jibril Balogun
Jibril Balogun - avatar
- 1
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
9th May 2020, 11:32 PM
Jibril Balogun
Jibril Balogun - avatar
- 1
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.
9th May 2020, 11:34 PM
Jibril Balogun
Jibril Balogun - avatar