Mickey Sawkiewicz
Mickey Sawkiewicz

Reputation: 51

How to call subroutines from user's choice

My Requirements

Write a program that draws one of five shapes depending on the user's choice: a line (l), a square (s), a rectangle (r), a triangle (t), or a diamond(d). If the user enters a valid choice, your program will prompt the user to enter the size of the shape.

Question

I've wrote the subroutines for each shape but I am having trouble figuring out how to use the users choice of letter to implement the subroutines. I assume I use if/else statements but am unsure how to use these with strings. This is the code I have so far:

#   Subroutines

def drawLine(length):
    length = int(input("Enter size of line: "))
    print("*" * (length))

def drawSquare(size):
    size = int(input("Enter size of side of square: "))
    for i in range(size):
        print('*' * size)

def drawRectangle(across_size, down_size):
    across_size = int(input("Enter the across side of rectangle: "))
    down_size = int(input("Enter the down side of rectangle: "))
    for i in range(down_size):
        for j in range(across_size):
            print('*' if i in [0, down_size-1] or j in [0, across_size-1] else " ", end='')
        print()

def drawTriangle(size):
    size = int(input("Enter size of triangle: "))
    x = 1
    while (x <= size):
        print("*" * x)
        x = x + 1

def drawDiamond(size):
    size = int(input("Enter size of diamond: "))
    for i in range(n-1):
        print((n-i) * ' ' + (2*i+1) * '*')
    for i in range(n-1, -1, -1):
        print((n-i) * ' ' + (2*i+1) * '*')

# main program
shape = input ("Enter type of shape (l,s,r,t,d): ")
list = ['l', 's', 'r', 't', 'd']

if shape not in list:
    print("Entered incorrect type of shape", shape)

I have created a list using the letters but can't continue my code so if someone selects 'l', it calls subroutine drawLine, etc.

Upvotes: 0

Views: 52

Answers (1)

M.G
M.G

Reputation: 440

The rest follow the same logic. You also need to get an integer from the user to pass into the function since you are giving input into your functions.

size = int(input("Please enter a number for the size: "))

if shape == 'l':
    drawLine(size)
else if shape == 's':
    drawSquare(size)
else if ...:
   .
   .
else:
     print("Entered incorrect type of shape")

As an aside, the way the function definition works here:

def drawLine(length):
    length = int(input("Enter size of line: "))
    print("*" * (length))

Is that you are defining a function called drawLine, which takes in 1 argument called length. When you call this function i.e drawLine(5), your function will execute with length = 5. When you then use the variable length inside the body of your function, it will be equal to whatever value you called the function with as it's argument, so you don't need to ask the user for input inside every function. Alternatively you could get the length inside of your functions, but then you should not be defining the function with any input parameters, like so:

# this definition takes 0 arguments(inputs) and gets length inside
def drawLine(): 
    length = int(input("Enter size of line: "))
    print("*" * (length))

# this definition expects drawLine to be called with a value, and you can get the length from the user elsewhere
def drawLine(length):
        print("*" * (length))

When called with the second definition, it will execute the body of your function as follows (using drawLine(10) as example):

print("*" * (10))

Upvotes: 1

Related Questions