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
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.
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**2==squares is checked for i=0 ,which is True so True is stored in the list at 0th position ,now number**2==square 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?
𝐊𝐢𝐢𝐛𝐨 𝐆𝐡𝐚𝐲𝐚𝐥 , 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.
Abhay I do not get it, explain further
https://code.sololearn.com/cpcC9BIVFswz/?ref=app My solution, checks all condition
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 i think you don't need that 1st loop
Sebastian Pacurar , can you look at this?
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
Thank you all for your inputs, I appreciate.
#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.
Problem Statement Given two integers a and b, your task is to calculate and print the following four values:
Please help, any idea?