Question

Write a Python function make_triangle(trianglechar, triangleheight) that will return a string with an isosceles right triangle...

Write a Python function make_triangle(trianglechar, triangleheight) that will return a string with an isosceles right triangle based on the two formal parameters triangle_height giving the triangle height and triangle_char that gives the symbol to be printed. Important notes: This program returns (not prints!) a string. For this function and the other part of this lab you are required to put in a docstring. That will be checked by a TA/grader reading the program. Every one of the occurrences of triangle_char should have a single space after it, including the ones at the ends of lines. This is not too complex to write either using while or using for and range(). If you're comfortable with for() and range(), that might be a little easier. The two ways we thought of to do this are either using two loops, one nested inside the other, or using one loop and string multiplication. \n' is the newline character. Your returned string should have triangle_char of them. (One at the end of every "line".) Example output for make_triangle('*', 5) is the string:

%

% %

% % %

% % % %

% % % % % or, equivalently, % \n% % \n% % % \n% % % % \n% % % % % \n Note that the last character of each of those 5 lines is a space!

0 0
Add a comment Improve this question Transcribed image text
Answer #1
def make_triangle(trianglechar, triangleheight):
    s = ''
    for i in range(triangleheight):
        s += (trianglechar + ' ') * (i + 1) + '\n'
    return s


# Below code is used to test the code. ignore/remove the code below
print(make_triangle('%', 5))

Add a comment
Know the answer?
Add Answer to:
Write a Python function make_triangle(trianglechar, triangleheight) that will return a string with an isosceles right triangle...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • In C programming language: This program will output a right triangle based on user specified height...

    In C programming language: This program will output a right triangle based on user specified height triangleHeight and symbol triangleChar. (1) The given program outputs a fixed-height triangle using a * character. Modify the given program to output a right triangle that instead uses the user-specified triangleChar character. (1 pt) (2) Modify the program to use a nested loop to output a right triangle of height triangleHeight. The first line will have one user-specified character, such as % or *....

  • in Java This program will output a right triangle based on user specified height triangleHeight and...

    in Java This program will output a right triangle based on user specified height triangleHeight and symbol triangleChar. (1) The given program outputs a fixed-height triangle using a character. Modify the given program to output a right triangle that instead uses the user-specified trianglechar character. (1 pt) (2) Modify the program to use a nested loop to output a right triangle of height triangleHeight. The first line will have one user-specified character, such as % or* Each subsequent line will...

  • (In C++) The program will output a right triangle based on user specified height triangleHeight and...

    (In C++) The program will output a right triangle based on user specified height triangleHeight and symbol triangleChar. Modify the program to use a nested loop to output a right triangle of height triangleHeight. The 1st line will have one user-specified character, such as %or * Each subsequent line will have 1 additional user-specified character until the number in the triangle"s base reaches triangleHeight. Output a space after each user-specified character, including after the line"s last user-specified character. Example output...

  • Hello there, im trying to answer this question in c 4.22 LAB: Warm up: Drawing a...

    Hello there, im trying to answer this question in c 4.22 LAB: Warm up: Drawing a right triangle This program will output a right triangle based on user specified height triangleHeight and symbol triangleChar. (1) The given program outputs a fixed-height triangle using a * character. Modify the given program to output a right triangle that instead uses the user-specified triangleChar character. (1 pt) (2) Modify the program to use a nested loop to output a right triangle of height...

  • 8.4 in python function that checks whether a string is a valid password. Suppose the pas...

    8.4 in python function that checks whether a string is a valid password. Suppose the pas rules are as follows: . A password must have at least eight characters - A password must consist of only letters and digits. ■ A password must contain at least two digits. Write a program that prompts the user to enter a password and displays valid password if the rules are followed or invalid password otherwise (Occurrences of a specified character) Write a function...

  • 10. Write a Python program to check if a triangle is equilateral, isosceles or scalene. An...

    10. Write a Python program to check if a triangle is equilateral, isosceles or scalene. An equilateral triangle is a triangle in which all three sides are equal. A scalene triangle is a triangle that has three unequal sides An isosceles triangle is a triangle with (at least) two equal sides

  • write a function which takes a string argument and a character argument. It should return a...

    write a function which takes a string argument and a character argument. It should return a truth value (int 0 or 1), 0 if the string does not contain the character, and 1 if the string does contain the character. Do not use a built-in library for this. Again, call this function and its variables whatever you deem appropriate. The main function of the program should accept a single character followed by Enter. Then, it will read lines until the...

  • MIPS programming question Problem 1: Write a program that asks the user to input a string...

    MIPS programming question Problem 1: Write a program that asks the user to input a string (or no more than 50 characters). Your program should then output the length of the string. The string length should be determined using a separate function strlen that will accept the address of the string and return its length. For the purposes of this exercise, the length of a string will be defined as the number of non-null and non-newline characters until either the...

  • On Python 3.6, write a function char_table(s), where s is a string. The function should produce...

    On Python 3.6, write a function char_table(s), where s is a string. The function should produce a table that shows the characters in the string and the number of occurrences of each character. Make the table nicely formatted, using right justification for the numbers (check out the format command.). List the characters in order (the order in the character encoding). Give some examples of the output.

  • Write a function oneSpaceAfterPeriod( String Parameter ) that will return a string with one space after...

    Write a function oneSpaceAfterPeriod( String Parameter ) that will return a string with one space after every period. Another function twoSpaceAfterPeriod (String Parameter) that will return a string with two spaces after every period.

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT