Question

repl,it HW 11.2 Please verify your em back to classroom run i.mport random 2 # DO NOT DELETE you may want to collapse it. 4-
Introduction Lists are just one of the ways that we can keep track of multiple values in order. Strings and ranges are some o
Examples Example 1 - sum_numbers () Example function call: sum_numbers (154 2000 7 85 Example return value: 2246 Example 2-
0 0
Add a comment Improve this question Transcribed image text
Answer #1

The required code :-

import random
# DO NOT DELETE - you may wan to collapse it.
def generate_number_strings():
"""Generates a string that contains digits and spaces."""
  
numbers = ""
rng = range(random.randint(1,20))
  
for i in rng:
if (len(numbers) > 0 \
and numbers[-1:] != " "\
and i != rng.stop -1 \
and random.randint(0,10) % 5 == 0):
numbers += " "
else:
numbers += str(random.randint(0,9))
return numbers

def sum_numbers(stringNumbers):
sumOfNos =0
  
# Split the string into individual numbers
listOfStrings = stringNumbers.split(' ')
  
# For each number add the number to sum
for number in listOfStrings:
sumOfNos = sumOfNos + int(number)
  
return sumOfNos


# The main() function
def main():
print("Enter the number of times you would like to execute the code :- ")
userInput = int(input())
  
# Call the sumOfNos as many times as the user input
for i in range(0,userInput):
  
# Get the random generate string of numbers
string = generate_number_strings()
  
# Find the sum using the sum_numbers function
sumOfNos = sum_numbers(string)
  
# print the output
print("The sum of "+string+" is "+str(sumOfNos))
  
if __name__== "__main__":
main()

Screenshot of Code for proper indentation

Sample Output Screenshot

Add a comment
Know the answer?
Add Answer to:
repl,it HW 11.2 Please verify your em back to classroom run i.mport random 2 # DO...
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
  • : back to classroom run Instructions from your teacher: import random def get_message(user_num, rand_num): Eo von...

    : back to classroom run Instructions from your teacher: import random def get_message(user_num, rand_num): Eo von AwNP Complete the get_message function so if user_num is equal to rand_num, return "You picked the same number as the computer!" if user_num is less than rand_num, return "Your number is smaller than the computer's number." if user_num is greater than rand_num, return "Your number is higher than the computer's number." Comment out the call to main when trying to pass the tests. 11...

  • Please write a code in Java where it says your // your code here to run...

    Please write a code in Java where it says your // your code here to run the code smoothly. Import statements are not allowed. _ A Java method is a collection of statements that are grouped together to perform an operation. Some languages also call this operation a Function. When you call the System.out.println() method, for example, the system actually executes several statements in order to display a message on the console. Please write the code according to functions assigned...

  • Please write a code in Java where it says your // your code here to run...

    Please write a code in Java where it says your // your code here to run the code smoothly. Import statements are not allowed for this assignment, so don't use them. _ A Java method is a collection of statements that are grouped together to perform an operation. Some languages also call this operation a Function. When you call the System.out.println() method, for example, the system actually executes several statements in order to display a message on the console. Please...

  • please use python and provide run result, thank you! click on pic to make it bigger...

    please use python and provide run result, thank you! click on pic to make it bigger For this assignment you will have to investigate the use of the Python random library's random generator function, random.randrange(stop), randrange produces a random integer in the range of 0 to stop-1. You will need to import random at the top of your program. You can find this in the text or using the online resources given in the lectures A Slot Machine Simulation Understand...

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

  • I need help with this assignment in C++, please! *** The instructions and programming style detai...

    I need help with this assignment in C++, please! *** The instructions and programming style details are crucial for this assignment! Goal: Your assignment is to write a C+ program to read in a list of phone call records from a file, and output them in a more user-friendly format to the standard output (cout). In so doing, you will practice using the ifstream class, I'O manipulators, and the string class. File format: Here is an example of a file...

  • Do this using the C language. show me the code being executed and also copy and...

    Do this using the C language. show me the code being executed and also copy and paste the code so i can try it out for myseld Instructions A cipher is mirrored algorithm that allow phrases or messages to be obfuscated (ie. "scrambled"). Ciphers were an early form of security used to send hidden messages from one party to another. The most famous and classic example of a cipher is the Caesar Cipher. This cypher worked by shifting each letter...

  • This C++ Program consists of: operator overloading, as well as experience with managing dynamic memory allocation...

    This C++ Program consists of: operator overloading, as well as experience with managing dynamic memory allocation inside a class. Task One common limitation of programming languages is that the built-in types are limited to smaller finite ranges of storage. For instance, the built-in int type in C++ is 4 bytes in most systems today, allowing for about 4 billion different numbers. The regular int splits this range between positive and negative numbers, but even an unsigned int (assuming 4 bytes)...

  • Lab 6 Instructions Objectives: • Executing and experimenting with programs that handle array related topics such...

    Lab 6 Instructions Objectives: • Executing and experimenting with programs that handle array related topics such as declaration, initialization, storing, retrieving, and processing elements. • Effectively manipulating and using ArrayList objects. • Becoming familiar with the use of arrays as method arguments and how array elements are passed to and returned from the called method. • Mastering the use of various debugging tools available on the Eclipse IDU. Important: EVERY one of the following Activities MUST be in a separate...

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