Computer Science
Find the error. Consider the following code and predict the error(s):
y for y in range(100) if y % 2 == 0 and if y % 5 == 0
Linear Lists
1 Like
Answer
- The list comprehensions should be enclosed in square brackets.
- The code contains a syntax error as it is using two
ifstatements within the list comprehension. We should use a singleifstatement with both conditions combined using theandoperator.
Answered By
2 Likes
Related Questions
Find the Error. Consider the following code, which runs correctly at times but gives error at other times. Find the error and its reason.
Lst1 = [23, 34, 12, 77, 34, 26, 28, 93, 48, 69, 73, 23, 19, 88] Lst2 = [] print("List1 originally is: ", Lst1) ch = int(input("Enter 1/2/3 and \ predict which operation was performed?")) if ch == 1: Lst1.append(100) Lst2.append(100) elif ch == 2: print(Lst1.index(100)) print(Lst2.index(100)) elif ch == 3: print(Lst1.pop()) print(Lst2.pop())Suggest the correction for the error(s) in previous question's code.
Find the error. Consider the following code and predict the error(s):
(y for y in range(100) if y % 2 == 0 and if y % 5 == 0)Find the error in the following list comprehension :
["good" if i < 3: else: "better" for i in range(6)]