Question

The problem in the Starting Out with Phython, 4th Edition, textbook solutions for Chpt 7, PE...

The problem in the Starting Out with Phython, 4th Edition, textbook solutions for Chpt 7, PE 1 is different than in the textbook.

Here is my program. The exact instructions is in the comments. Here is what I want to know. 1) is this correct? 2) is this the most efficient way to create the output.

#Chapter 7
#Exercise 1

#design a program that uses a loop to build a list named
#valid_numbers that contains only the numbers between
#0 and 100 from the numbers list below. The program
#should then determine and display the total and average
#of the values in the valid_numbers list.

def main():

numbers = [74, 19, 105, 20, -2, 67, 77, 124, -45, 38]

valid_numbers = []
total = 0
average = 0
counter = 0
  
for x in numbers:
if x >= 0 and x <= 100:
valid_numbers.append(x)
total += x
counter += 1

average = total / counter

print('The total is: ', total, ' and the average is: ', average, sep='')

main()

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

The program seems correct, and it gives true results.

It has a time complexity of O(n), which is significantly good for such a problem.

The output can be optimized by rounding off the digits up to two decimal places.

It is done in the provided code.


#Chapter 7
#Exercise 1

# design a program that uses a loop to build a list named
# valid_numbers that contains only the numbers between
# 0 and 100 from the numbers list below. The program
# should then determine and display the total and average
# of the values in the valid_numbers list.

def main():

  numbers = [74, 19, 105, 20, -2, 67, 77, 124, -45, 38]

  valid_numbers = []
  total = 0
  average = 0
  counter = 0
    
  for x in numbers:
    if x >= 0 and x <= 100:
      valid_numbers.append(x)
      total += x
      counter += 1

  average = round(total/counter,2)

  print('The total is: ', total, ' and the average is: ', average, sep='')

main()

Add a comment
Know the answer?
Add Answer to:
The problem in the Starting Out with Phython, 4th Edition, textbook solutions for Chpt 7, PE...
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
  • #Starting Out With Python, 4th Edition #Chapter 7 #Exercise 6 #in a program, write a function...

    #Starting Out With Python, 4th Edition #Chapter 7 #Exercise 6 #in a program, write a function named roll that #accepts an integer argument number_of_throws. The #function should generate and return a sorted #list of number_of_throws random numbers between #1 and 6. The program should prompt the user to #enter a positive integer that is sent to the function, #and then print the returned list. How would you do this?

  • Starting out with python 4th edition Write program that lets the user enter in a file...

    Starting out with python 4th edition Write program that lets the user enter in a file name (numbers.txt) to read, keeps a running total of how many numbers are in the file, calculates and prints the average of all the numbers in the file. This must use a while loop that ends when end of file is reached. This program should include FileNotFoundError and ValueError exception handling. Sample output: Enter file name: numbers.txt There were 20 numbers in the file....

  • Starting out with Python 4th edition I need help I followed a tutorial and have messed...

    Starting out with Python 4th edition I need help I followed a tutorial and have messed up also how does one include IOError and IndexError exception handling? I'm getting errors: Traceback (most recent call last): File, line 42, in <module> main() File , line 37, in main winning_times = years_won(user_winning_team_name, winning_teams_list) File , line 17, in years_won for winning_team_Index in range(len(list_of_teams)): TypeError: object of type '_io.TextIOWrapper' has no len() Write program champions.py to solve problem 7.10. Include IOError and IndexError...

  • Python Programming 4th Edition: Need help writing this program below. I get an error message that...

    Python Programming 4th Edition: Need help writing this program below. I get an error message that says that name is not defined when running the program below. My solution: def main(): filename = "text.txt" contents=f.read() upper_count=0 lower_count=0 digit_count=0 space_count=0 for i in contents: if i.isupper(): upper_count+=1 elif i.islower(): lower_count+=1 elif i.isdigit(): digit_count+=1 elif i.isspace(): space_count+=1 print("Upper case count in the file is",upper_count) print("Lower case count in the file is",lower_count) print("Digit count in the file is",digit_count) print("Space_count in the file is",space_count)...

  • Starting out with Python 4th edition I need help with while loops I can't even get...

    Starting out with Python 4th edition I need help with while loops I can't even get one started correctly. Write a program a program that predicts the approximate size of a population of organisms. Problem 4.13 – population Use a while loop; there are no text boxes, just regular input statements Enter number of organisms: 2 Enter average daily increase: 30 Enter number of days to multiply: 10 DAY       APPROXIMATE POPULATION ------------------------------------------------- 1            2 2            2.600 3            3.380 4            4.394...

  • Chapter 7, Problem 5PP in Starting out with Visual C# (4th Edition) World Series Champions Teams.txt...

    Chapter 7, Problem 5PP in Starting out with Visual C# (4th Edition) World Series Champions Teams.txt - This ile contains a list of several Major League baseball teams in alphabetical order. Each team listed in the file has one the World Series at least once. WorldSeriesWinners.txt - This file contains a chronological list of the World Series' winning teams from 1903 through 2012. Create an application that displays the contents of the Teams.txt file in a ListBox control. When the...

  • Using R program: Read and try out the list of comments and examples in Appendix A:...

    Using R program: Read and try out the list of comments and examples in Appendix A: A Sample Session in the "An Introduction to R" textbook. Then solve the following exercises in R. Use the help function if needed in the increments of 1.Generate a vector a between 0 and 100 Create a vector b with 100 pseudo-random numbers in it Plot a on the x-axis and b on the y-axis. Make a data frame of two columns a and...

  • this code is not working for me.. if enter 100 it is not showing the grades...

    this code is not working for me.. if enter 100 it is not showing the grades insted asking for score again.. #Python program that prompts user to enter the valuesof grddes . Then calculate the total scores , #then find average of total score. Then find the letter grade of the average score. Display the #results on python console. #grades.py def main(): #declare a list to store grade values grades=[] repeat=True #Set variables to zero total=0 counter=0 average=0 gradeLetter='' #Repeat...

  • hello help me asap please kindly "Programming Challenge 1 -- Going Green," of Starting Out with...

    hello help me asap please kindly "Programming Challenge 1 -- Going Green," of Starting Out with Programming Logic and Design. Note: You are only required to create the flowchart for this activity; however, notice how the pseudocode compares to the given Python code for this assignment. Lab 9: Arrays This lab accompanies Chapter 8 of Gaddis, T. (2016). Starting out with programming logic and design (4th ed.). Boston, MA: Addison-Wesley. Lab 9.5 – Programming Challenge 1 -- Going Green Write...

  • I need help in Python. This is a two step problem So I have the code...

    I need help in Python. This is a two step problem So I have the code down, but I am missing some requirements that I am stuck on. Also, I need help verifying the problem is correct.:) 7. Random Number File Writer Write a program that writes a series of random numbers to a file. Each random number should be in the range of 1 through 500. The application should let the user specify how many random numbers the file...

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