Question

Write a python function called gen_pattern that accepts a string as an argument. Your function should...

Write a python function called gen_pattern that accepts a string as an argument. Your function should generate a string pattern based on argument provided:

  • If the string provided is a number and that number is even generate a pattern of hash signs. ex. gen_pattern(‘4’): ####

  • If the string provided is odd, generate pattern of stars. Ex: gen_pattern(3): ***

  • If string is not a number just output “ Not a number”

0 0
Add a comment Improve this question Transcribed image text
Answer #1
def gen_pattern(s):
    try:
        num = int(s)
        if(num%2==0):
            print("#" * num)
        else:
            print("*" * num)
    except Exception as e:
        print("Not a number")

#Testing
gen_pattern("4")
gen_pattern(3)
gen_pattern("a")
Add a comment
Know the answer?
Add Answer to:
Write a python function called gen_pattern that accepts a string as an argument. Your function should...
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
  • 1. Write a function called ordinal_sum that accepts a string argument and returns the sum of...

    1. Write a function called ordinal_sum that accepts a string argument and returns the sum of the ordinal values of each character in the string. The ordinal value of a character is its numeric Unicode code point in decimal. 2. Write code that creates a Python set containing each unique character in a string named my_string. For example, if the string is 'hello', the set would be {'h', 'e', 'l', 'o'} (in any order). Assign the set to a variable...

  • Python please Write a function print_back_upper() that accepts a string my_str as an argument and displays...

    Python please Write a function print_back_upper() that accepts a string my_str as an argument and displays the string backwards and in uppercase, all in one line (Note: Create any necessary variables) For example: if my_str = "Python" then, your output should display as follows: NOHTYP (Hint: You can use for loop navigating up to range of len of string-1, -1, -1 and use reverse() and upper() functions to create and display the string backwards in uppercase)

  • Write a function that accepts a pointer to a C-string as its argument. The function should...

    Write a function that accepts a pointer to a C-string as its argument. The function should count the number of times the character 'w’ occurs in the argument and return that number.

  • python Problem 3 (Palindrome) Write a function called ispalindrome(string) that takes a string (with spaces) as...

    python Problem 3 (Palindrome) Write a function called ispalindrome(string) that takes a string (with spaces) as argument and returns True if that string (ignoring spaces) is a palindrome. A palindrome is a word or phrase that spells the same thing forwards as backwards (ignoring spaces). Your program should use a recursive process to determine the result, by comparing the first and last letters of the string, and if necessary, calling ispalindrome() on the rest of the string. Sample run: print(ispalindrome('never...

  • Java Programming Write a method named isEven that accepts an integer argument. The method should return...

    Java Programming Write a method named isEven that accepts an integer argument. The method should return true if the argument is even, or false if not. The program’s main method should use a loop to generate a random integer. The number of random integers is provided by a user, who must enter a number greater than 10. The loop should then invoke the isEven method to determine whether each integer is even or odd, display the random number and the...

  • Define a Python function named getResponse() that takes a string as its only argument and returns...

    Define a Python function named getResponse() that takes a string as its only argument and returns a new string. If the argument ends with a question mark and has an even number of characters, the function should return the string "Yes". If the argument ends with a question mark and contains an odd number of characters, the function should return the string "No". If the argument ends with an exclamation point, the function should return the string "Wow". Otherwise, the...

  • Write a program with a function that accepts a string as an argument and returns a...

    Write a program with a function that accepts a string as an argument and returns a copy of the string with the first character of each sentence capitalized. For instance, if the argument is “hello. my name is Joe. what is your name?” the function should return the string “Hello. My name is Joe. What is your name?” The program should let the user enter a string and then pass it to the function. The modified string should be displayed.

  • please use python Question 4: Define a function named q40 that accepts a string as a...

    please use python Question 4: Define a function named q40 that accepts a string as a parameter. After verifying that the string includes only includes numeric digits, count the number of even (E) and odd (O) digits. Finally, create a new string made up of these values and their sum. Repeat this process until your end result is a string containing 123 For example, '15327' contains 1 even digit (E-1), 4 odd digits(O-4) and the sum of these is 5...

  • Write a Python function, called counting, that takes two arguments (a string and an integer), and...

    Write a Python function, called counting, that takes two arguments (a string and an integer), and returns the number of digits in the string argument that are not the same as the integer argument. Include a main function that inputs the two values (string and integer) and outputs the result, with appropriate labelling. You are not permitted to use the Python string methods (such as count(), etc.). Sample input/output: Please enter a string of digits: 34598205 Please enter a 1-digit...

  • 1. Write a Python function that accepts a string and calculate the number of upper case...

    1. Write a Python function that accepts a string and calculate the number of upper case letters and lower case letters. Sample String : 'The quick Brow Fox' Expected Output : No. of Upper case characters : 3 No. of Lower case Characters : 12

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