Новый курс! Каждый программист должен знать генеративный ИИ!
Попробуйте бесплатный урок0
How it write like this in Python???
Spam Eggs Spam Eggs Spam Eggs What should be the code for this????
13 ответов
+ 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
+ 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!!
+ 1
print("Spam")
print("Eggs")
Etc.....
There are other ways, but start with this method
+ 1
Eco Fico
I know , and u should try to solve this once , that's the way u learn new things by doing mistakes.
+ 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
+ 1
Flandre Scarlet 😁👍
Awesome examples
+ 1
Print("spam")
Print("egg")
Print("spam")
Print("egg")
I think that will do the trick
+ 1
Mvd suq red you
0
Did u tried it at least once????
then u should attach your code here.
0
DEVIL I'm asking how can we do it. I don't how to do it.
0
Rik Wittkopp I wanna know the other way.
0
Rik Wittkopp I tried this one.
0
Print("spam")
Print("eggs)
Print("spam")
Print("eggs")
Print("spam")
Print("eggs)