Home > Blockchain >  Display message hello with "for" instead of "while"
Display message hello with "for" instead of "while"

Time:11-14

So my professor is saying that I can do this easier with 'for' instead of 'while' but I have no idea how to do it.

The code below output the message hello with the number it was inputed. another problem that I have is that the message hello is displayed going down is there a way to make it go from left to right. Thanks and sorry for asking many question. I am just getting started doing coding.

pr_char = input('number to display?:')
h = [[1,0,0,0,1],
     [1,0,0,0,1],
     [1,1,1,1,1],
     [1,0,0,0,1],
     [1,0,0,0,1]]
i=0
while i < len(h):
    j=0
    while j<len(h[i]):
        if h[i][j] ==1:
            print(pr_char, end='')
        else:
            print(' ',end='')
        j = j   1
    print()
    i = i 1

e = [[1,1,1,1,1],
     [1,0,0,0,0],
     [1,1,1,1,1],
     [1,0,0,0,0],
     [1,1,1,1,1]]
i=0
while i < len(e):
    j=0
    while j<len(e[i]):
        if e[i][j] ==1:
            print(pr_char, end='')
        else:
            print(' ',end='')
        j = j   1
    print()
    i = i 1

l = [[1,0,0,0,0],
     [1,0,0,0,0],
     [1,0,0,0,0],
     [1,0,0,0,0],
     [1,1,1,1,1]]
i=0
while i < len(l):
    j=0
    while j<len(l[i]):
        if l[i][j] ==1:
            print(pr_char, end='')
        else:
            print(' ',end='')
        j = j   1
    print()
    i = i 1

l1 = [[1,0,0,0,0],
     [1,0,0,0,0],
     [1,0,0,0,0],
     [1,0,0,0,0],
     [1,1,1,1,1]]
i=0
while i < len(l1):
    j=0
    while j<len(l1[i]):
        if l1[i][j] ==1:
            print(pr_char, end='')
        else:
            print(' ',end='')
        j = j   1
    print()
    i = i 1

o = [[0,1,1,1,0],
     [1,0,0,0,1],
     [1,0,0,0,1],
     [1,0,0,0,1],
     [0,1,1,1,0]]
i=0
while i < len(o):
    j=0
    while j<len(o[i]):
        if o[i][j] ==1:
            print(pr_char, end='')
        else:
            print(' ',end='')
        j = j   1
    print()
    i = i 1```

CodePudding user response:

A construct like

i = 0
while i < j:
    ...
    i  = 1

is equivalent to

for i in range(0, j):
    ...
  • Related