Question

Question 5 Next, you are asked to implement a Python function called compareScoreStrings(), that will take in the score strin

Python Language.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Python code:

#initializing compareScoreString function

def compareScoreString(p1String,p2String):

    #converting p1String to a list of numbers and storing it in p1list

    p1list=list(map(int,p1String.split(",")))

    #converting p2String to a list of numbers and storing it in p2list

    p2list=list(map(int,p2String.split(",")))

    #used to store number of levels player 1 have a perfect score

    p1level=0

    #used to store number of levels player 2 have a perfect score

    p2level=0

    #looping each score in p1list

    for i in p1list:

        #checking each score in p1list for perfect score

        if(i==9):

            #incrementing p1level

            p1level+=1

    #looping each score in p2list

    for i in p2list:

        #checking each score in p2list for perfect score

        if(i==9):

            #incrementing p2level

            p2level+=1

    #finding average score of p1

    p1avg=sum(p1list)/len(p1list)

    #finding average score of p2

    p2avg=sum(p2list)/len(p2list)

    #checking if p1 has highest average score

    if(p1avg>p2avg):

        #returning '1' and p1level

        return('1',p1level)

    #else p2 has highest average score

    else:

        #returning '2' and p2level

        return('2',p2level)

#testing and printing

print(compareScoreString("1,2,3,4,5,6,7,8,9","1,2,3,4,5,6,7"))

Screenshot:

ON 1 #initializing compareScoreString function 2. def compareScoreString(P1String, p2string): 3 #converting pistring to a lis

Output:

Your Output (stdout) 1 (i, 1)

Add a comment
Know the answer?
Add Answer to:
Python Language. Question 5 Next, you are asked to implement a Python function called compareScoreStrings(), that...
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 python: ScoreFinder is a function that takes in two lists and a string. The first...

    In python: ScoreFinder is a function that takes in two lists and a string. The first list is a list of strings (player names), the second list is a list of floats (player scores), and the string is a name (player to find). If the player to find exists in the list of player names, then print the name of that player along with their associated score (which is in the second list at the same index). If the player...

  • 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...

  • Write a python program write a function numDigits(num) which counts the digits in int num. Answer...

    Write a python program write a function numDigits(num) which counts the digits in int num. Answer code is given in the Answer tab (and starting code), which converts num to a str, then uses the len() function to count and return the number of digits. Note that this does NOT work correctly for num < 0. (Try it and see.) Fix this in two different ways, by creating new functions numDigits2(num) and numDigits3(num) that each return the correct number of...

  • urgent Help needed in python program ! Thanx # This is a function definition. You can...

    urgent Help needed in python program ! Thanx # This is a function definition. You can ignore this for now. def parse_integer_list_from_string(string): """ Returns a list of integers from a string containing integers separated by spaces. """ # Split the line into a list of strings, each containing a number. number_string_list = string.split() # Create an empty list to store the numbers as integers. numbers = [] # Convert each string to an integer and add it to the list...

  • language is python Write a function named subsetStrings that has two inputs, a list of strings...

    language is python Write a function named subsetStrings that has two inputs, a list of strings and an integer n. Your function should use a single list comprehension to create a new list containing the first n characters of each string in the input list if the length of the individual string is at least n (strings that are too short should be skipped). Your function should return the new list ex: inputList 'Frederic, 'powerade', 'spring break, 'pen'] and n-4...

  • Write the following program in C Language using standard library functions. /*Implement the Find function, called...

    Write the following program in C Language using standard library functions. /*Implement the Find function, called in the program below. This function receives two strings, and looks for the first occurrence of the second string in the first string, returning the number of characters it is in, relative to the beginning of the first string. If not, returns -1. In the program, a string with two news headlines is given, and the sub-strings "iPad" and "Huawei” are searched for, having...

  • Please write code in Python! You are asked to implement the following checksum formula to validate...

    Please write code in Python! You are asked to implement the following checksum formula to validate an identifi- cation number given to you. The formula works as follows. Using the original number, double the value of every other digit. Then add the values of the individual digits together (if a doubled value now has two digits, add the digits individually). The identification number is valid if the resulting sum is divisible by 10. Write a function called validateID that takes...

  • Assignment 2 – The two player game of Poaka The game of Poaka is a dice...

    Assignment 2 – The two player game of Poaka The game of Poaka is a dice game played by two players. Each player takes turns at rolling the dice. At each turn, the player repeatedly rolls a dice until either the player rolls a 1 (in which case the player scores 0 for their turn) or the player chooses to end their turn (in which case the player scores the total of all their dice rolls). At any time during...

  • This CSIS 9 Python. Java Python Warmup-2 > string_times prev next | chance Given a string...

    This CSIS 9 Python. Java Python Warmup-2 > string_times prev next | chance Given a string and a non-negative int n, return a larger string that is n copies of the original string. string_times('Hi', 2) – 'HiHi' string_times('Hi', 3) - 'HiHiHi' string_times('Hi', 1) – 'Hi' Solution: Go Save, Compile, Run (ctrl-enter) Show Solution def string_times (str, n): def string_times(str, n): result = "" for i in range(n): # range(n) is [0, 1, 2, .... n-1] result = result + str...

  • In this project, you will use functions and dictionaries to track basketball players and their respective...

    In this project, you will use functions and dictionaries to track basketball players and their respective points, then display statistics of points made. You will need three functions as follows: def freeThrowMade(playerDictionary, playerName) - this function will add 1 point to the player's total score def twoPointMade(playerDictionary, playerName) - this function will add 2 points to the player's total score def threePointMade(playerDictionary, playerName) - this function will add 3 points to the player's total score Each of these functions has...

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