How to call subroutines from user's choice












0














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.










share|improve this question



























    0














    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.










    share|improve this question

























      0












      0








      0







      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.










      share|improve this question













      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.







      python method-call






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Nov 23 at 2:25









      Mickey Sawkiewicz

      263




      263
























          1 Answer
          1






          active

          oldest

          votes


















          1














          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))





          share|improve this answer























            Your Answer






            StackExchange.ifUsing("editor", function () {
            StackExchange.using("externalEditor", function () {
            StackExchange.using("snippets", function () {
            StackExchange.snippets.init();
            });
            });
            }, "code-snippets");

            StackExchange.ready(function() {
            var channelOptions = {
            tags: "".split(" "),
            id: "1"
            };
            initTagRenderer("".split(" "), "".split(" "), channelOptions);

            StackExchange.using("externalEditor", function() {
            // Have to fire editor after snippets, if snippets enabled
            if (StackExchange.settings.snippets.snippetsEnabled) {
            StackExchange.using("snippets", function() {
            createEditor();
            });
            }
            else {
            createEditor();
            }
            });

            function createEditor() {
            StackExchange.prepareEditor({
            heartbeatType: 'answer',
            autoActivateHeartbeat: false,
            convertImagesToLinks: true,
            noModals: true,
            showLowRepImageUploadWarning: true,
            reputationToPostImages: 10,
            bindNavPrevention: true,
            postfix: "",
            imageUploader: {
            brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
            contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
            allowUrls: true
            },
            onDemand: true,
            discardSelector: ".discard-answer"
            ,immediatelyShowMarkdownHelp:true
            });


            }
            });














            draft saved

            draft discarded


















            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53439956%2fhow-to-call-subroutines-from-users-choice%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown

























            1 Answer
            1






            active

            oldest

            votes








            1 Answer
            1






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes









            1














            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))





            share|improve this answer




























              1














              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))





              share|improve this answer


























                1












                1








                1






                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))





                share|improve this answer














                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))






                share|improve this answer














                share|improve this answer



                share|improve this answer








                edited Nov 23 at 2:50

























                answered Nov 23 at 2:27









                M.G

                388310




                388310






























                    draft saved

                    draft discarded




















































                    Thanks for contributing an answer to Stack Overflow!


                    • Please be sure to answer the question. Provide details and share your research!

                    But avoid



                    • Asking for help, clarification, or responding to other answers.

                    • Making statements based on opinion; back them up with references or personal experience.


                    To learn more, see our tips on writing great answers.





                    Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


                    Please pay close attention to the following guidance:


                    • Please be sure to answer the question. Provide details and share your research!

                    But avoid



                    • Asking for help, clarification, or responding to other answers.

                    • Making statements based on opinion; back them up with references or personal experience.


                    To learn more, see our tips on writing great answers.




                    draft saved


                    draft discarded














                    StackExchange.ready(
                    function () {
                    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53439956%2fhow-to-call-subroutines-from-users-choice%23new-answer', 'question_page');
                    }
                    );

                    Post as a guest















                    Required, but never shown





















































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown

































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown







                    Popular posts from this blog

                    Lallio

                    Unable to find Lightning Node

                    Futebolista