简体   繁体   English

我想打印一个字母模式,如果有什么东西可以即兴创作我用 python 编写的代码

[英]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.因此,如果我想printgrocery_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 ,然后printvariable 。”

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.

相关问题 在 Python 中,我想打印同心正方形,因为我已经编写了代码,但我没有得到想要的 output - In Python, i want to print concentric square for that i have written a code but i am not getting desired output 我已经为数字的collat​​z模式编写了一个python代码,我想计算达到1所需的步骤数。我该怎么做? - I have written a python code for the collatz pattern for number and I want to count the number of steps it took to reach 1. How can I do it? 我已经在 python 2 中编写了一个代码,现在我想在 python3 中执行它,我收到错误 - I have written one code in python 2 now i want to execute this in python3, i am getting error PYTHON:每当我输入一些东西时,我希望我的代码打印多个可能的值 - PYTHON: Whenever I input something, I want my code to print multiple possible possible values 我想在python中的while循环中只打印一次 - I want to print something only once in a while loop in python 我想检查输入是否为python代码 - I would like to check if an input is python code 我使用 python 创建了一个排行榜,但我的代码打印在一行中。 我希望它像排行榜格式一样单独打印每个元素 - I have created a leaderboard using python but my code prints in a single line. I want it to print each element separately like in a leaderboard format 我想在C ++中创建类似python字典的东西 - I want to create something like a python dictionary in C++ 我想使用python删除/清除打印队列 - I would like to delete/clear the print queue using python Python - 为什么 __str__ 在调用时想要返回一些东西? 我在 __str__ 中有打印功能 - Python - Why does __str__ want something returned when it called? I have print functions inside of __str__
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM