How it write like this in Python??? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

How it write like this in Python???

Spam Eggs Spam Eggs Spam Eggs What should be the code for this????

22nd Apr 2020, 4:43 AM
Eco Fico
Eco Fico - avatar
13 Answers
+ 17
There are many methods From easiest to harder: 1. print for each print("Spam") print("Eggs") print("Spam") print("Eggs") print("Spam") print("Eggs") 2. Loops for i in range(3): print("Spam") print("Eggs") 3. Loops with If-Else for i in range(6): if i%2==0: print("Spam") else: print("Eggs") 4. List Comprehension [print("Spam") if i%2==0 else print("Eggs") for i in range(6)] 5. List Comprehension 2 [print("Spam" if i%2==0 else "Eggs") for i in range(6)] 6. Map with Lambda-Function and Range [*map(lambda i: print(["Spam", "Eggs"][i%2]), range(6))] 7. Map with Function and List-Multiplying [*map(print, ["Spam", "Eggs"]*3)] There are still a lot other methods
22nd Apr 2020, 9:24 AM
Flandre Scarlet
Flandre Scarlet - avatar
+ 3
#Method 1 (using list comprehension) x=[print("Eggs","Spam",sep="\n") for i in range(3)] #Method 2 for i in range(3): print("Eggs","Spam",sep="\n") I hope it helps!!
22nd Apr 2020, 5:01 AM
ANJALI SAHU
+ 1
print("Spam") print("Eggs") Etc..... There are other ways, but start with this method
22nd Apr 2020, 4:59 AM
Rik Wittkopp
Rik Wittkopp - avatar
+ 1
Eco Fico I know , and u should try to solve this once , that's the way u learn new things by doing mistakes.
22nd Apr 2020, 4:59 AM
Raj Laxmi Verma
Raj Laxmi Verma - avatar
+ 1
for i in range(6): If i%2: print("Eggs") else: print("Spam") If you already know the basics, then continue your course to learn more as suggested by DEVIL
22nd Apr 2020, 5:03 AM
Rik Wittkopp
Rik Wittkopp - avatar
+ 1
Flandre Scarlet 😁👍 Awesome examples
22nd Apr 2020, 9:32 AM
Rik Wittkopp
Rik Wittkopp - avatar
+ 1
Print("spam") Print("egg") Print("spam") Print("egg") I think that will do the trick
22nd Apr 2020, 9:12 PM
AVATAR
AVATAR - avatar
+ 1
Mvd suq red you
23rd Apr 2020, 9:53 PM
Abdulwahab Sajo Mienda
Abdulwahab Sajo Mienda - avatar
0
Did u tried it at least once???? then u should attach your code here.
22nd Apr 2020, 4:50 AM
Raj Laxmi Verma
Raj Laxmi Verma - avatar
0
DEVIL I'm asking how can we do it. I don't how to do it.
22nd Apr 2020, 4:53 AM
Eco Fico
Eco Fico - avatar
0
Rik Wittkopp I wanna know the other way.
22nd Apr 2020, 5:00 AM
Eco Fico
Eco Fico - avatar
0
Rik Wittkopp I tried this one.
22nd Apr 2020, 5:00 AM
Eco Fico
Eco Fico - avatar
0
Print("spam") Print("eggs) Print("spam") Print("eggs") Print("spam") Print("eggs)
23rd Apr 2020, 10:33 AM
Dev Dev
Dev Dev - avatar