我需要使用O
来反转打印三角形的输出格式我的代码是
userRows=int(input("Enter a positive number less than or equal to 20!"));
while((userRows>21) or (userRows<0)):
userRows=int(input("Try again! Please enter a positive number less than or equal to 20."));
rows=1;
while(rows<=userRows):
columns=1;
while(columns<=rows):
print("O",end='');
columns=columns+1;
print('');
rows=rows + 1;
输出
Enter a positive number less than or equal to 20!20
O
OO
OOO
OOOO
OOOOO
OOOOOO
OOOOOOO
OOOOOOOO
OOOOOOOOO
OOOOOOOOOO
OOOOOOOOOOO
OOOOOOOOOOOO
OOOOOOOOOOOOO
OOOOOOOOOOOOOO
OOOOOOOOOOOOOOO
OOOOOOOOOOOOOOOO
OOOOOOOOOOOOOOOOO
OOOOOOOOOOOOOOOOOO
OOOOOOOOOOOOOOOOOOO
OOOOOOOOOOOOOOOOOOOO
我试图让它从右到左对齐,而不是从左到右(如上图所示)。
答案 0 :(得分:0)
在打印'o'之前打印n(用户输入 - 迭代次数)空白
def displayTriganle(userInput):
[print(' '*(userInput-i)+ 'o'*i) for i in range(1,userInput+1)]
o
oo
ooo
oooo
ooooo
oooooo
ooooooo
oooooooo
ooooooooo
oooooooooo
ooooooooooo
oooooooooooo
ooooooooooooo
oooooooooooooo
ooooooooooooooo
oooooooooooooooo
ooooooooooooooooo
oooooooooooooooooo
ooooooooooooooooooo
oooooooooooooooooooo
答案 1 :(得分:0)
>>> userRows=int(input("Enter a positive number less than or equal to 20!"));
Enter a positive number less than or equal to 20!20
>>> for i in range(userRows):
... print ' '*(userRows-i)+'0'*i
...
0
00
000
0000
00000
000000
0000000
00000000
000000000
0000000000
00000000000
000000000000
0000000000000
00000000000000
000000000000000
0000000000000000
00000000000000000
000000000000000000
0000000000000000000