Home > database >  Letter Arrow with For Loop in Python
Letter Arrow with For Loop in Python

Time:03-26

I have a homework to make an arrow letter with nested for loop in Python. The goal is to make an arrow that starts with input letter and the input maximum size; for example, input C for letter and 5 for size the output should be like this

C
D D
E E E
F F F F
G G G G G
H H H H H
I I I I
J J J
K K
L

I'm having a problems with making it starts with the input letter, and make it have maximum for 2 rows. This is what I got so far (since when I change the variable to make it starts with input letter the code is error).

letter = (input("What is the starting letter? "))
size = int(input("What is the maximum size of the arrow? Please enter an integer: "))


val = 65
for x in range(0,size):
    for y in range (0,x 1):
        char = chr(val)
        print (char, end = " ")
    val = val   1
    print()

for x in range(size,0,-1):
    for y in range (0,x-1):
        char = chr(val)
        print (char, end = " ")
    val = val   1
    print()

When I run this, it look like this

What is the starting letter? C
What is the maximum size of the arrow? Please enter an integer: 5
A 
B B 
C C C 
D D D D 
E E E E E 
F F F F 
G G G 
H H 
I 

CodePudding user response:

the reason why it starts with A is because u have val set to 65 i.e ascii value for A

use

val = ord(letter)

instead of

val = 65

this should now work as intended

ord - returns the string to its ascii value of the string

CodePudding user response:

The letter variable has not been used in the rest of the code. You should do something like:

letter = (input("What is the starting letter? "))
size = int(input("What is the maximum size of the arrow? Please enter an integer: "))


val = ord(letter)
for x in range(0,size):
    for y in range (0,x 1):
        char = chr(val)
        print (char, end = " ")
    val = val   1
    print()

for x in range(size,0,-1):
    for y in range (0,x-1):
        char = chr(val)
        print (char, end = " ")
    val = val   1
    print()

Example output

What is the starting letter? C
What is the maximum size of the arrow? Please enter an integer: 5
C 
D D 
E E E 
F F F F 
G G G G G 
H H H H 
I I I 
J J 
K 

Note that, this won't work with an input like Z as the character and 4 as the number.

Wrong Output

What is the starting letter? Z
What is the maximum size of the arrow? Please enter an integer: 4
Z 
[ [ 
\ \ \ 
] ] ] ] 
^ ^ ^ 
_ _ 
` 

CodePudding user response:

I missed the point in setting variables, at first. The result is all correct now, and it looks like this

letter = (input("What is the starting letter? "))
size = int(input("What is the maximum size of the arrow? Please enter an integer: "))

val = ord(letter)
for x in range(0,size):
    for y in range (0,x 1):
        char = chr(val)
        print (char, end = " ")
    val = val   1
    print()

for x in range(size,0,-1):
    for y in range (0,x):
        char = chr(val)
        print (char, end = " ")
    val = val   1
    print()

changing the val = 65 to val = ord(letter) and change thex-1to x to make 2 rows of maximum. Thank you for all the answers.

  • Related