[英]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] 这样的图像的代码,我可以在其中输入长度和宽度的单位
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你看到那空白的倒数第二行..我一直试图摆脱它一段时间但如果我删除
\ni 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-1
和p-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.