+ 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
17 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.
+ 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!
+ 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
+ 2
I think Abhay has done a good job explaining this.
Or what exactly are you trying to do with the code?
+ 1
Lothar This is the code
👑 Tchybooxuur! Look at this
+ 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.
+ 1
Abhay I do not get it, explain further
+ 1
https://code.sololearn.com/cpcC9BIVFswz/?ref=app
My solution, checks all condition
+ 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())
+ 1
Dr. Exceptional i think you don't need that 1st loop
0
Sebastian Pacurar , can you look at this?
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
0
Thank you all for your inputs, I appreciate.
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")
- 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.
- 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
- 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.