如何通过在python中使用while循环来打印以下模式?

问题描述:

如何在python中使用while循环打印以下模式?如何通过在python中使用while循环来打印以下模式?

! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! \ \ ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! !// \ \ \ \ ! ! ! ! ! ! ! ! ! ! ! ! ! !//// \ \ \ \ \ \ ! ! ! ! ! ! ! ! ! !////// \ \ \ \ \ \ \ \ ! ! ! ! ! !//////// \ \ \ \ \ \ \ \ \ \ ! !//////////

任何帮助将非常感激。谢谢。

试试这个:

ExcCount = 22 
SlashCount = 2 

i = 1 
while i <= 6: 

    String = (SlashCount * "\ ") + (ExcCount * "! ") + (SlashCount * "/ ") 
    print(String) 

    ExcCount -= 4 
    SlashCount -= 2 
    i += 1 
+0

您的代码似乎没有打印任何东西,并进入无限循环。 – ritiek

+0

@Ritiek Soz,忘了增加'我' – Adi219

这也可以使用for循环解决,但因为这个问题明确提到while循环:

total = 22 
n = 0 

while n < 6: 
    side = n*2 
    middle = total - side*2 
    line = '\\'*side + '!'*middle + '/'*side 
    print(line) 
    n += 1 

让我知道,如果有什么需要解释。

有两种简单的方法来做到这一点

while True: 
    print '! '*22 
    print '\ '*2+'! '*18+'/ '*2 
    print '\ '*4+'! '*14+'/ '*4 
    print '\ '*6+'! '*10+'/ '*6 
    print '\ '*8+'! '*6+'/ '*8 
    print '\ '*10+'! '*2+'/ '*10 
    break 

也许

x=0 
while x<6: 
    print '\ '*(2*x)+'! '*(22-(4*x))+'/ '*(2*x) 
    x+=1