简体   繁体   English

在 python 中生成以下输出(for 和 while 循环)?

[英]Produce the following output (for and while loops) in python?

I have an assignment about for and while loops and I am just confused with some stuff.我有一个关于 for 和 while 循环的作业,我只是对一些东西感到困惑。 It would be much appreciated if you guys can help me.如果你们能帮助我,我将不胜感激。

The question is produce the following out using for and while loops in python:问题是在 python 中使用 for 和 while 循环产生以下结果:

1
FredFredFredFred
2
FredFredFred
3
FredFred
4
Fred

this is what I have so far for a "for loop"这就是我到目前为止的“for循环”

for i in range(1,5):
    print(i)
    print('Fred'*i)

but I can't get the 'Fred' to be like what they asked us for.但我不能让“弗雷德”像他们要求的那样。

Printing Fred one lest time each iteration can be controlled by multiplying the string by a decreasing value (in this case, 5-i ).通过将字符串乘以一个递减的值(在本例中为5-i ),可以控制每次迭代最多打印Fred一次。

for i in range(1,5):
     print(i)
     print("Fred"*(5-i))

This outputs:这输出:

1
FredFredFredFred
2
FredFredFred
3
FredFred
4
Fred

Using While loop使用 While 循环

i = 1
while (i < 5):
   print(i)
   print('fred'*(5-i))
   i = i + 1

Output:输出:

1
fredfredfredfred
2
fredfredfred
3
fredfred
4
fred

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM