简体   繁体   English

如何在 go 到 python 3.x 中的特定行的开头进行更改呢?

[英]How to go to the beginning of a specific line in python 3.x to make changes in it?

Is there aa way to go up a line ( like a opposite \n ) or to go to the start of a specific line ( say line number = 9 )有没有一种方法可以将 go 向上一行(如相反的 \n )或 go 到特定行的开头(例如行号 = 9 )

Given below is the code I tried to create images like [fig] where I could input units of lenght and breadth下面给出的是我试图创建像 [fig] 这样的图像的代码,我可以在其中输入长度和宽度的单位

code代码

well, the out for a certain case looks like..[fig]好吧,某个案例的出局看起来像..[图]

enter the length unit as integer(not more than 20):5
enter the breadth unit as integer(not more than 20) :5

* * * * * 
*        *
*        *
*        *

* * * * * 

you see that blank second-last line.. i have been trying to get rid of it for sometime but if i remove你看到那空白的倒数第二行..我一直试图摆脱它一段时间但如果我删除

 \n 
i dont know a way to proceed.. so i thought if i could jump up to that line, i could solve the issue somehow.. 我不知道如何继续..所以我想如果我能跳到那条线,我可以以某种方式解决这个问题..

{ i checked similar questions here and tried the suggested methods but it didn't work ( eg: goto, "\033[F" )} {我在这里检查了类似的问题并尝试了建议的方法,但没有奏效(例如:goto,“\033[F”)}

ps: i am a beginner in python. ps:我是 python 的初学者。

First, some general code review.首先,一些一般性的代码审查。 I think the unnecessary complexity of your code is part of the problem.我认为您的代码不必要的复杂性是问题的一部分。 The construct you're using in your code:您在代码中使用的构造:

def symbol():
    box()
def box():
    # Your actual code
symbol()

isn't doing anything useful beyond running # Your actual code .除了运行# Your actual code之外没有做任何有用的事情。 You can get rid of all the function definitions and just write the section I've called # Your actual code in each case.您可以摆脱所有 function 定义,只需编写我称为#Your # Your actual code in each case的部分。 Also, your middle two sections run exactly the same code so don't need to be separate.此外,您中间的两个部分运行完全相同的代码,因此不需要分开。

What you need to do to remove the excess newline is pass the option end="" to print() ;删除多余的换行符所需要做的是将选项end=""传递给print() this removes the default newline on the end of the print statement.这将删除打印语句末尾的默认换行符。

Final code, excluding the difference between the cases which are use p-1 and p-2 , is below.最终代码,不包括使用p-1p-2的情况之间的差异,如下所示。

x = int(input("Enter length (≤20): "))
y = int(input("Enter height (≤20): "))

symb = "*"

if x > 20 or y > 20:
    print("error")
else:
    row = (symb + " ") * x
    print(row)
    central = (symb + (" " * (len(row) - 1)) + symb + "\n") * (y - 2)
    print(central, end="")
    print(row)

When run this produces (for inputs 12, 10):运行时会产生(对于输入 12、10):

Enter length (≤20): 12
Enter height (≤20): 10
* * * * * * * * * * * * 
*                       *
*                       *
*                       *
*                       *
*                       *
*                       *
*                       *
*                       *
* * * * * * * * * * * * 

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

相关问题 如何在python 3.x中按行打印字符串 - How to print strings in line in python 3.x Python 2.x 到 3.x - Append 语法错误:如何使其与 Python 3.x 从 2.x 兼容。 - Python 2.x to 3.x - Append Syntaxerror: how to make it compatibale with Python 3.x from 2.x? 如何在 Python 3.x 中逐行打印网页 - How can I print a webpage line by line in Python 3.x Python 3.x-如果要退出程序,则在y / n条件下如何使否答案返回程序的开头? - Python 3.x - If quitting program, at the y/n conditional how do I make the no answer go back to the start of program? 如何在 Python 3.x 中传递命令行参数? - How to pass command line arguments in Python 3.x? 如何在 Python 3.x 中检查字符串中的新行? - How can I check for a new line in string in Python 3.x? 在使用Python 3.x的脚本中,使用Python 2.7导入其他脚本时,如何在导入时使该特定脚本与2.7分开运行? - In a script using Python 3.x, importing other script using Python 2.7, how to make that specific script running with 2.7 separately when importing it? 代码从Python 2.6更改为3.x - Code changes from Python 2.6 to 3.x 如何使用Python 3.x转换特定程序的文件扩展名? - How to convert a file extension with a specific program by using Python 3.x? 如何使 python 3.x 在 web 浏览器中输入文本 - how to make python 3.x enter text into a web browser
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM