简体   繁体   中英

How to use a symbol as input for a function in Python

I'm attempting to create a function that creates a rectangle made of an inputted character. So,

def Make_character_rectangle(height, width, char):

char = str(char)
while height > 0:
    height = height-1
    print(t * width)

So Make _character_rectangle(3, 2, %) should make:

%%

%%

%%

however when i input a symbol such as % into the function, it says invalid syntax. I tried converting char into a string but it still gives the same error.

def Make_character_rectangle(height, width, char):

    char = str(char)
    while height > 0:
        height = height-1
        print(char * width)

Not sure what 't' was, but the above function now produces

>>> Make_character_rectangle(2,3,'%')
%%%
%%%

I don't see where you defined t in print(t * width). Change the t to char.

You can also change height = height - 1 to height -= 1.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM