Question

Write two new functions that simulate the addition and subtraction operators. Each of these functions should accept a width v

use python please

Write two new functions that simulate the addition and subtraction operators. Each of these functions should accept a width value as an argument (integer) and a single character (string) -- the function should then return the generated pattern. You can assume the operators will always be 5 units high. Here's some sample code:

temp = plus(5, '*')
print(temp)
print()

temp = minus(5, '*')
print(temp)

Which will generate ...

  *
  *
*****
  *
  *



*****


Note that your "plus" sign may look odd if it is rendered using an even size value - for example:

# rendered using a width of 6
  *
  *
******
  *
  *

To fix this you should double up the vertical line in the center for even sizes, like this:

# rendered using a width of 6
  **
  **
******
  **
  **
0 0
Add a comment Improve this question Transcribed image text
Answer #1

Code Snippet :

Pattern.py def plus (num, str): pattern = #Empty String if num2==1: #if it is odd number for i in range(5): #As height is

Raw Code :

def plus(num,str):
   pattern = '' #Empty String
   if num%2==1: #if it is odd number
       for i in range(5): #As height is always 5 range is fixed
           if i!=2:
               pattern += (' %s \n' %str) #Adding the str pattern into pattern variable
           else:
               pattern += (str*num + '\n') #Generating str pattern of length of given num value
   else: #if it is even number
       for i in range(5):
           if i!=2:
               pattern += (' %s%s \n' %(str,str)) #Adding the str pattern for even length into pattern variable
           else:
               pattern += (str*num + '\n') #Generating str pattern of length of given num value
   return pattern.rstrip() #rstrip() is used to remove the new line at the end

def minus(num,str):
   return str*num #returning the str pattern multiplied by its num value

temp = plus(5,'*') #Calling the function and storing its returned value into temp var
print(temp) #Printing pattern
print()

temp = minus(5,'*')
print(temp)

# temp = plus(6,'*')
# print(temp)

Output :

py3 Pattern.py

Code Snippet :

Pattern.py def plus (num, str): pattern = #Empty String if num2==1: #if it is odd number for i in range(5): #As height is

Raw Code :

def plus(num,str):
   pattern = '' #Empty String
   if num%2==1: #if it is odd number
       for i in range(5): #As height is always 5 range is fixed
           if i!=2:
               pattern += (' %s \n' %str) #Adding the str pattern into pattern variable
           else:
               pattern += (str*num + '\n') #Generating str pattern of length of given num value
   else: #if it is even number
       for i in range(5):
           if i!=2:
               pattern += (' %s%s \n' %(str,str)) #Adding the str pattern for even length into pattern variable
           else:
               pattern += (str*num + '\n') #Generating str pattern of length of given num value
   return pattern.rstrip() #rstrip() is used to remove the new line at the end

def minus(num,str):
   return str*num #returning the str pattern multiplied by its num value

# temp = plus(5,'*') #Calling the function and storing its returned value into temp var
# print(temp) #Printing pattern
# print()

# temp = minus(5,'*')
# print(temp)

temp = plus(6,'*')
print(temp)

Output :

Add a comment
Know the answer?
Add Answer to:
use python please Write two new functions that simulate the addition and subtraction operators. Each of...
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
  • Please Use C++ for coding . . Note: The order that these functions are listed, do...

    Please Use C++ for coding . . Note: The order that these functions are listed, do not reflect the order that they should be called. Your program must be fully functional. Submit all.cpp, input and output files for grading. Write a complete program that uses the functions listed below. Except for the printodd function, main should print the results after each function call to a file. Be sure to declare all necessary variables to properly call each function. Pay attention...

  • programming language: C++ *Include Line Documenatations* Overview For this assignment, write a program that will simulate...

    programming language: C++ *Include Line Documenatations* Overview For this assignment, write a program that will simulate a game of Roulette. Roulette is a casino game of chance where a player may choose to place bets on either a single number, the colors red or black, or whether a number is even or odd. (Note: bets may also be placed on a range of numbers, but we will not cover that situation in this program.) A winning number and color is...

  • Write a C PROGRAM that will simulate rolling a die. When rolling a die, one will...

    Write a C PROGRAM that will simulate rolling a die. When rolling a die, one will get a die face value of 1-6. We can use rand function and math ceiling function to get a random number of 1-6 generated. The program will simulating rolling 5 dice simultaneously and store their face values into an array of size 5. The program will keep rolling the dice and print the following: 1. All face values in one line 2. If any...

  • Python 3+ (using TKinter): 1.) Develop new TKinter GUI widget class Ed that can be used to teach first-graders addition and subtraction. The GUI should contain two Entry widgets and a Button widget la...

    Python 3+ (using TKinter): 1.) Develop new TKinter GUI widget class Ed that can be used to teach first-graders addition and subtraction. The GUI should contain two Entry widgets and a Button widget labeled "Enter". At start-up, your program should generate (1) two single-digit pseudorandom numbers a and b and (2) an operation op, which could be addition or subtraction—with equal likelihood—using the randrange() function in the random module. The expression a op b will then be displayed in the...

  • Using Python 3+ for Question P5.9 Instructions: Use one main() to call each of the functions...

    Using Python 3+ for Question P5.9 Instructions: Use one main() to call each of the functions you created. Only use one value of r and h for all the function. Ask the user for the r and h in the main() as an input, and h for all the functions. You should put the functions for areas in a separate file. ​ For example, firstDigtec7ze Write a function e digits n) (returning the number of digits in the argument returning...

  • Python 3: Please follow the respective rubric for the following function definition. Rubric: Rectangle Shape You...

    Python 3: Please follow the respective rubric for the following function definition. Rubric: Rectangle Shape You should implement the following functions to print a rectangle shape. • print_rectangle(length, width, fillChar, hollow). This function will use draw_shape_line() function to print a rectangle shape with the given length and width, If the hollow is true, the shape will be hollow rectangle, otherwise a filled rectangle. This function must not interact with the user. For example, the call print_rectangle(5, 10, '*', False) should...

  • Note: The order that these functions are listed, do not reflect the order that they should...

    Note: The order that these functions are listed, do not reflect the order that they should be called. Your program must be fully functional. Submit all .cpp, input and output files for grading. Write a complete program that uses the functions listed below. Except for the printOdd function, main should print the results after each function call to a file. Be sure to declare all necessary variables to properly call each function. Pay attention to the order of your function...

  • use python pls Previously, an X was used to represent every integer value. However, we don't...

    use python pls Previously, an X was used to represent every integer value. However, we don't have space to represent large values using the approach (e.g., it would be unreadable if the values were in the hundreds or thousands). Therefore, we need to scale the bars so that each X represents a larger quantity We will display the bars as percentages, where the largest value in the data set is treated as 100%. The height of the other bars will...

  • ***************************************PLEASE USE AN ARRAY NOT A POINTER******************************************** Detailed Specification: Write six basic functions for the BST:...

    ***************************************PLEASE USE AN ARRAY NOT A POINTER******************************************** Detailed Specification: Write six basic functions for the BST: Insert, Delete, Search, Find max, Find min, and Print_BST 1. Search(x): Find out the index that stores element x using binary search tree mechanism. Print out all the elements in the search path. 2. Find max(): Find and print maximum value in BST 3. Find min): Find and print minimum value in BST 4. Print BST: Print out the BST structure in the form...

  • Write a complete program that uses the functions listed below. Except for the printOdd function, main...

    Write a complete program that uses the functions listed below. Except for the printOdd function, main should print the results after each function call to a file. Be sure to declare all necessary variables to properly call each function. Pay attention to the order of your function calls. Be sure to read in data from the input file. Using the input file provided, run your program to generate an output file. Upload the output file your program generates. •Write a...

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