如何在python中为循环设置等于的变量

如何在python中为循环设置等于的变量,python,for-loop,python-3.x,Python,For Loop,Python 3.x,这是我的密码。如何使bownew=多行*?我不想只打印范围(高度)中的x:打印新的。我希望bownew在范围(高度)上等于x的值。如何做到这一点 height = int(input("Enter an odd number greater than 4: ")); column = height * 2; screen = []; bownew = "" def bow(height): for x in range(height): screen.append([

这是我的密码。如何使bownew=多行*?我不想只打印范围(高度)中的x:打印新的。我希望bownew在范围(高度)上等于x的值。如何做到这一点

height = int(input("Enter an odd number greater than 4: "));
column = height * 2;

screen = [];
bownew = ""

def bow(height):
    for x in range(height):
        screen.append(["*"]*column);

bow(height);

for i in screen:
    bownew = " ".join(i)
print(bownew)

不要将
用于
循环,
join
希望列表作为参数。如果你想让它有多行,用换行符连接它们

bownew = "\n".join(screen)
您还需要将
屏幕
设置为字符串列表,而不是列表列表:

def bow(height):
    for x in range(height):
        screen.append("*" * column);
整个剧本:

height = int(input("Enter an odd number greater than 4: "));
column = height * 2;

screen = [];
bownew = ""

def bow(height):
    for x in range(height):
        screen.append("*" * column);

bow(height);

bownew = "\n".join(screen)

print(bownew)
试运行:

$ python test.py
Enter an odd number greater than 4: 5
**********
**********
**********
**********
**********

1。可能有一个更简单的解决方案,它不需要使用for循环

height = int('Enter an odd number greater than 4: ')
column = height * 2

row = '*' * column
bownew = [row] * height
bownew = '\n'.join(bownew)

print(bownew)
,我已经测试过了,它很有效

Enter an odd number greater than 4: 5
**********
**********
**********
**********
**********
2。至于问题(如何在python中将变量设置为for loop?),我怀疑除非使用
def
,否则无法使变量等于任何循环

def
的使用将被禁用

height = int('Enter an odd number greater than 4: ')
column = height * 2

def bownew(height, column):
    for i in range(height):
        print('*' * column)

bownew(height, column)
这应该会给你同样的结果


愉快的编码,希望这有帮助。

但我希望有多行。应该有输入的高度变量作为行数,用换行符而不是空格连接。它不起作用。表示预期字符串,但得到列表。我认为问题在于创建
屏幕的方式。这是一个列表列表,而不是字符串列表。尝试使用
“*”*column
而不是
[“*”]*column
仍然不工作“list obj不能是interp.as int”在Python中不需要分号(至少你是如何使用它们的)。是否只是在一行上有多个代码行?@paulandshadow是的,这是唯一需要它们的时候。欢迎使用StackOverflow!请直接在答案中输入代码,而不是在图像中输入。