Question

Python: Create a function count_target_in_list that takes a list of integers and the target value then...

Python:

Create a function count_target_in_list that takes a list of integers and the target value then counts and returns the number of times the target value appears in the list.

Write program in that uses get_int_list_from_user method to get a list of 10 numbers, then calls the count_target_list method to get the count then prints the number of times the target was found in the list.

def get_int_list_from_user():
    lst=[]
    y = int(input("Enter number of numbers"))
    for x in range(y):
        lst.append(int(input("Enter next whole number: ")))
    return lst
0 0
Add a comment Improve this question Transcribed image text
Answer #1

Answer:

Here is the program:

def get_int_list_from_user(): 1st=[] y = int(input(Enter number of numbers: )) for x in range(y): lst.append(int(input(Ent


def get_int_list_from_user():
lst=[]
y = int(input("Enter number of numbers: "))
  
for x in range(y):
lst.append(int(input("Enter next whole number: ")))
  
return lst

def count_target_in_list(lst, target):
count=0
for i in lst:
if target==i:
count=count+1
return count


lst = get_int_list_from_user()
print(count_target_in_list(lst, 3))

Output:

Enter number of numbers: 5 Enter next whole number: 3 Enter next whole number: 1 Enter next whole number: 2 Enter next whole

PLEASE UPVOTE IF YOU FIND THIS HELPFUL!

Add a comment
Know the answer?
Add Answer to:
Python: Create a function count_target_in_list that takes a list of integers and the target value then...
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
  • Write a function that takes, as an argument, a list of positive integers and a target...

    Write a function that takes, as an argument, a list of positive integers and a target value, and returns the number of times that the target value appears in the list. Call this function problem1(myList, target). For example, >>>problem1([1,2,3,4,5,6,5,4,3], 5) should return 2, and >>>problem1([1,2,3,4,5,6,5,4,3], 7) should return 0.

  • write a Python function that takes in a list of integers and returns maximum and minimum values in the list as a tuple

    1 write a Python function that takes in a list of integers and returns maximum and minimum values in the list as a tuple. Hint (can be done in one pass, you are not allowed to use built-on min and max functions.)max, min = find_max_min(my_list):2 write a Python function that takes in a list of integers (elements), and an integer number (num). The functions should count and return number of integers in elements greater than, less than, and equal to...

  • PYTHON The function longest that returns the longest of two strings. The function count_over that takes...

    PYTHON The function longest that returns the longest of two strings. The function count_over that takes a list of numbers and an integer nand returns the count of the numbers that are over n. The function bmi that takes two parameters height and weight and returns the value of the body mass index: a person's weight in kg divided by the square of their height in meters. def longest(string1, string2): """Return the longest string from the two arguments, if the...

  • In Java* ​​​​​​​ Write a program that reads an arbitrary number of 20 integers that are...

    In Java* ​​​​​​​ Write a program that reads an arbitrary number of 20 integers that are in the range 0 to 100 inclusive. The program will ask a user to re-enter an integer if the user inputs a number outside of that range. The inputted integers must then be stored in a single dimensional array of size 20. Please create 3 methods: 1. Write a method public static int countEven(int[] inputArray) The method counts how many even numbers are in...

  • Write function all_coprime_pairs( ) which takes as input a list, L, with positive unique integers (i.e....

    Write function all_coprime_pairs( ) which takes as input a list, L, with positive unique integers (i.e. no duplicated integers), and should return: 1. List (of tuples) containing all pairs of numbers in L that are coprime. 2. Empty list, If no pair of numbers in L is coprime. Your solution must include a function call to the function coprime designed in the previous question. The order of the tuples or the order of the two numbers within the tuples is...

  • Python homework Write a function called insert_value(my_list, value, insert_position) that takes a list, a value and...

    Python homework Write a function called insert_value(my_list, value, insert_position) that takes a list, a value and an insert_position as parameters. The function returns a copy of the list with the value inserted into the list (my_list) at the index specified by insert_position. Check for the insert_position value exceeding the list (my_list) bounds. if the insert_position is greater than the length of the list, insert the value at the end of the list. if the insert_position is less than or equal...

  • 1) Translate the following equation into a Python assignment statement 2) Write Python code that prints...

    1) Translate the following equation into a Python assignment statement 2) Write Python code that prints PLUS, MINUS, O ZERO, depending on the value stored in a variable named N. 3) What is printed by: 3 - 1 while 5: while 10 Print ) Page 1 of 9 4) Write a Python while loop that reads in integers until the user enters a negative number, then prints the sum of the numbers. 1-2 of 9 4) Write a Python while...

  • 1. use python List to Dictionary Write a function that has three parameters: a list of...

    1. use python List to Dictionary Write a function that has three parameters: a list of unsorted numbers with no duplicates, a start number, and an end number. This function should return a dictionary with all integers between the start and end number (inclusive) as the keys and their respective indices in the list as the value. If the integer is not in the list, the corresponding value would be None. Example unsorted list: [2,1,10,0,4,3] two numbers: 3, 10 returned...

  • Use the FDR to design and write a function, maxValTimes, that takes a list of integers...

    Use the FDR to design and write a function, maxValTimes, that takes a list of integers and returns a set containing the value(s) that occur the same number of times as the maximum value in the list. If the list is empty, return an empty set.   For example if the list was [2,1,1,2,3,3,1] the function would return {2,3} as the maximum value is 3 which occurs twice, and 2 also occurs twice (but 1 occurs 3 times). For full marks,...

  • Python. Write a function that takes in a list and returns the first nonzero entry. def...

    Python. Write a function that takes in a list and returns the first nonzero entry. def nonzero(lst): """ Returns the first nonzero element of a list >>> nonzero([1, 2, 3]) 1 >>> nonzero([0, 1, 2]) 1 >>> nonzero([0, 0, 0, 0, 0, 0, 5, 0, 6]) 5 """ "*** YOUR CODE HERE ***"

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