Home > OS >  how to code something that requires printing with lines
how to code something that requires printing with lines

Time:04-02

a is a parameter in def star(a). I have to define a def and coding. I'm asking a= int(input("what is a?: ") for example if a is 3: first line has one sign second line has two sign third line has 3 sign and done.

def star(a):
    #idk
    return #dk
a=int(input("a is a:  "))
print("Shape:  ", star(a))

CodePudding user response:

You can just multiply strings in python using *!

def print_line(number_of_signs):
    print("*"*number_of_signs 1) # plus one since it otherwise prints 0 starts at first iteration


a = int(input("a is a: "))
for line in range(a):
    print_line(line)

If you want the stars to start in the middle, you should add some logic in print_line to form a string, than print that string.

edit: added 1 in print_line to deal with the first iteration being 0.

CodePudding user response:

Your question isn't the clearest, but I'll try to answer nonetheless.

def star(a):
    # Will only work if a is odd
    if (a % 2) == 0:
        print("a should be odd!")
        return None

    output = ""  # Final output string
    for i in range(1, a   1, 2):                                     # This represents the number of signs per line, must always be odd so start at 1 and increment by 2
        padding = int((a - i) / 2)                                   # This is the padding on either side of the signs

        line = (" " * padding)   ("-" * i)   (" " * padding)   "\n"  # Create the line, \n creates a new line

        output  = line                                               # Add the line to the final output

    return output


a = int(input("a > "))
print(star(a))

Output:

a > 5
  -  
 --- 
-----

Is that what you were going for?

  • Related