Python 帮助修改此代码删除';*';运算符,并改用while循环?
如何修改我的代码并删除“*”运算符,我使用该运算符对字符串进行乘法,并使用while循环而不是它来生成相同的模式Python 帮助修改此代码删除';*';运算符,并改用while循环?,python,python-2.7,while-loop,Python,Python 2.7,While Loop,如何修改我的代码并删除“*”运算符,我使用该运算符对字符串进行乘法,并使用while循环而不是它来生成相同的模式 i = 1 x = 1 while i <= 4: print "v"*x print "v"*x x = x+1 i+=1 print "v"*5 b = 4 while b>=1: print "v"*b print "v"*b b=b-1 i=1 x=1 当i=1时: 打印“v”*b 打印“v”*b b=b-
i = 1
x = 1
while i <= 4:
print "v"*x
print "v"*x
x = x+1
i+=1
print "v"*5
b = 4
while b>=1:
print "v"*b
print "v"*b
b=b-1
i=1
x=1
当i=1时:
打印“v”*b
打印“v”*b
b=b-1
> p>也许考虑在while循环中使用<代码>范围>代码>函数和<代码> >循环< /代码>。range(stop,end)
函数返回一个数组,其中包含从stop
(包含)到end
(独占)的整数。例如:范围(0,3)
返回[0,1,2]
。因此,在for循环
中,您可以通过在外部while循环的每次迭代中为从范围
返回的数组的每个成员打印一个v,从而打印出x
的v数或b
的v数。比如说像这样,
i = 1
x = 1
while i <= 4:
for y in range(0, x):
print "v"
print "v"
x = x+1
i+=1
print "v"*5
b = 4
while b>=1:
for y in range(0, b):
print "v"
print "v"
b=b-1
i=1
x=1
当i=1时:
对于范围(0,b)内的y:
打印“v”
打印“v”
b=b-1
嘿,这是最简单的方法
length = 4
i = 0
output = "v"
for j in range(length-1):
print output + "\n" + output
output += "v"
print output
for j in range(length):
output = output[:-1]
print output + "\n" + output
试试这个。希望这有帮助
如果只想使用while循环..以下是解决方案
length = 4
i = 1
output = "v"
flag = False
while (i < length):
if i == 0:
break
if not flag:
print output + "\n" + output
output += "v"
i+=1
else:
output = output[:-1]
print output + "\n" + output
i-=1
if i == length:
i-=1
print output
flag = True
length=4
i=1
输出=“v”
flag=False
而(i
希望这能解决问题。:) 有什么问题吗?换行符?@KarolyHorvath代码运行得很好,我只想在乘以字符串时使用while循环,而不是使用星形运算符(*)。是的,这似乎是一种很好的方法。但是我需要使用while循环而不是for循环。