[英]I want to print a pattern of letters and would like if there is something to improvise the code I have written in python
Below is the code, I would like if someone could improve this code further:以下是代码,我想如果有人可以进一步改进此代码:
i=0
while i<4:
if i==0:
print()
print(chr(65),end="")
i=i+1
if i==1:
print()
print(chr(65) ,end="")
print(chr(66) ,end="")
i=i+1
if i==2:
print()
print(chr(65), end="")
print(chr(66), end="")
print(chr(67), end="")
i=i+1
if i==3:
print()
print(chr(65), end="")
print(chr(66), end="")
print(chr(67), end="")
print(chr(68), end="")
i=i+1
output:输出:
A
AB
ABC
ABCD
Just use two for
loops:只需使用两个
for
循环:
for w in range(5):
for c in range(w):
print(chr(65+c), end="")
print()
The outer loop if for the length of the line and the inner for the characters.外部循环用于行的长度,内部用于字符。
You can use two for loops.您可以使用两个 for 循环。 1st loop for controlling the number of char you want in each line.
第一个循环,用于控制每行所需的字符数。 2nd loop prints characters.
第二个循环打印字符。
Try this.尝试这个。
for i in range(4):
for j in range(65,i+66):
print(chr(j),end='')
print()
Let me show you how you can use a FOR LOOP
correctly.让我向您展示如何正确使用
FOR LOOP
。 I'm going to use my own exercise and then YOU find a way to implement it into your code.我将使用我自己的练习,然后您会找到一种方法将其实现到您的代码中。
VARIABLES As you know, variables
can be assigned many many different values.变量如您所知,
variables
可以分配许多不同的值。 Of which, can be made into a list
.其中,可以做成一个
list
。
LISTS So if I wanted to create a grocery list I could write the code as follows: grocery_list = ["Apples", "Bananas", "Gummy Bears"]
LISTS所以如果我想创建一个购物清单,我可以编写如下代码:
grocery_list = ["Apples", "Bananas", "Gummy Bears"]
So now the variable
grocery_list
is equal to 3 separate strings.所以现在
variable
grocery_list
等于 3 个单独的字符串。
FOR LOOPS Now to do something to each item on that list
, (or any list
for that matter) we use something called a for loop
. FOR 循环现在要对该
list
(或任何与此相关的任何list
)上的每个项目执行某些操作,我们使用称为for loop
东西。 The for loop
creates only one temporary variable
to be used for each item in your list
in order. for loop
只创建一个临时variable
,按顺序用于list
中的每一项。
Now listen carefully, this may be clear in a second.现在仔细听,这可能一秒钟就清楚了。
Once the first item (which is assigned to a temporary variable
) is done, it moves on to the second item by assigning it to the same temporary variable
, effectively overwriting it.一旦第一项(分配给临时
variable
)完成,它通过将其分配给同一个临时variable
来移动到第二项,有效地覆盖它。
Here is the syntax of the for loop
followed by a more plain English translation.这是
for loop
的语法,后面是更简单的英文翻译。
for every_item in my_list:
do_this
Translates to this:翻译成这样:
"For every_item (one at a time, in order)
in my_list (that I've already created):
do the following with those items
which are assigned to the
variable, 'each_item'."
So if I wanted to print
out all of the items in my grocery_list
, I can reference the plain English and regular syntax examples I've written earlier.因此,如果我想
print
出grocery_list
所有项目,我可以参考我之前编写的纯英语和常规语法示例。
for each_item in grocery_list:
print(each_item)
"For every single item, in order, assign that particular item to this variable
, and then print
that variable
." “对于每个单独的项目,按顺序将该特定项目分配给该
variable
,然后print
该variable
。”
And again just to emphasize:再次强调:
"For every single item, (both in order AND only one at a time) assign to the FIRST item in the list to THIS variable I've decided to call: each_item. THEN, repeat for the SECOND item, followed by the third item and so on." “对于每个项目,(按顺序并且一次只有一个)将列表中的第一个项目分配给我决定调用的这个变量:each_item。然后,对第二个项目重复,然后是第三个项目等等。”
One more example Maybe with those items in my list
, I want to show that I have already purchased them.再举一个例子也许我的
list
那些物品,我想表明我已经购买了它们。 In that case I could write:在那种情况下,我可以写:
for each_item in grocery_list:
print(each_item + " purchased)
For loops are so ingrained in programming it's not even funny. For 循环在编程中根深蒂固,甚至都不好笑。 So please make sure that you read up as much as you can.
因此,请确保您尽可能多地阅读。 Hope this helped.
希望这有帮助。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.