Hi,
In this post i attached my lecture 5 which describes chapter 8,9. Here i am going to post practice questions for chapter 8.
here, you can find the lecture slides,
Programming exercises
you can find these exercise questions at the end of the chapter 8 in this book.
1. Total Sales
Design a program that asks the user to enter a store’s sales for each day of the week. The amounts should be stored in a list. Use a loop to calculate the total sales for the week and
display the result.
answer :-
saleList = []
print("store's sales for each day of the week ")
for i in range(7):
print("store sale of the day",(i + 1),": $",sep="",end="")
storeSale = float(input(""))
saleList.append(storeSale)
print("saleList : ",end="")
print(saleList)
print("calculating the total sales for the week....")
total = 0
for element in saleList:
total += element
print("total sales for the week : $",format(total,',.2f'),sep="")
2. Lottery Number Generator
Design a program that generates a seven-digit lottery number. The program should generate seven random numbers, each in the range of 0 through 9, and assign each number to a list element. Then write another loop that displays the contents of the list.
Design a program that generates a seven-digit lottery number. The program should generate seven random numbers, each in the range of 0 through 9, and assign each number to a list element. Then write another loop that displays the contents of the list.
answer :-
import random
lotteryList = []
print("assigning the lottery numbers to the list.....")
for i in range(7):
lotteryNumber = random.randint(0,9)
lotteryList.append(lotteryNumber)
print("lottery numbers are assigned to the list.....")
print("represting the lottery number....")
print("contents of the list : ",end="")
print("[ ",end="")
for element in lotteryList:
print(element ,end="")
print(" ]")
3. Rainfall Statistics
Design a program that lets the user enter the total rainfall for each of 12 months into a list. The program should calculate and display the total rainfall for the year, the average monthly rainfall, and the months with the highest and lowest amounts.
Design a program that lets the user enter the total rainfall for each of 12 months into a list. The program should calculate and display the total rainfall for the year, the average monthly rainfall, and the months with the highest and lowest amounts.
answer :-
rainfallList = []
print("store the rainfall for each month of the year ")
for i in range(12):
print("rainfall of the month ",(i + 1),": ",sep="",end="")
storeRainfall = float(input(""))
rainfallList.append(storeRainfall)
print("saleList : ",end="")
print(rainfallList)
print("calculating the total and average rainfall for the year....")
total = 0
for element in rainfallList:
total += element
averageRainfall = total / 12
print("total rainfall for the year : ",format(total,',.2f'),sep="")
print("average rainfall for the year : ",format(averageRainfall,',.2f'),sep="")
print("highest railfall of the year : ",max(rainfallList))
print("lowest railfall of the year : ",min(rainfallList))
4. Number Analysis Program
Design a program that asks the user to enter a series of 20 numbers. The program shouldstore the numbers in a list and then display the following data:
Design a program that asks the user to enter a series of 20 numbers. The program shouldstore the numbers in a list and then display the following data:
• The lowest number in the list
• The highest number in the list
• The total of the numbers in the list
• The average of the numbers in the list
answer :-
numberslList = []
print("store the 20 numbers in a list ")
for i in range(20):
print("Enter the number ",(i + 1),": ",sep="",end="")
numbers = float(input(""))
numberslList.append(numbers)
print("numberslList : ",end="")
print(numberslList)
print("finding the lowest,highest,total and average of the list....")
total = 0
for element in numberslList:
total += element
averageNumbers = total / 20
print("The lowest number in the list :",min(numberslList))
print("The highest number in the list :",max(numberslList))
print("The total of the numbers in the list : ",format(total,',.2f'),sep="")
print("The average of the numbers in the list : ",format(averageNumbers,',.2f'),sep="")
5. Charge Account ValidationIf you have downloaded the source code from this book’s companion Web site, you will find a file named charge_accounts.txt in the Chapter 08 folder. This file has a list of a company’s valid charge account numbers. Each account number is a seven-digit number, such as 5658845.
Write a program that reads the contents of the file into a list. The program should then ask the user to enter a charge account number. The program should determine whether the number is valid by searching for it in the list. If the number is in the list, the program should display a message indicating the number is valid. If the number is not in the list, the program should display a message indicating the number is invalid.
answer :-
accountList = []
file_vari = open("charge_accounts.txt","r")
for i in file_vari:
j = i.rstrip("\n")
accountList.append(j)
file_vari.close()
print("accountList = ",end="")
print(accountList)
accountCheck = input("Enter the charge account number : ")
if accountCheck in accountList:
print("account number that you entered is valid")
else:
print("account number that you entered isnot valid")
6. Driver’s License Exam
The local driver’s license office has asked you to create an application that grades the written portion of the driver’s license exam. The exam has 20 multiple-choice questions. Here are the correct answers:
The local driver’s license office has asked you to create an application that grades the written portion of the driver’s license exam. The exam has 20 multiple-choice questions. Here are the correct answers:
1. B 6. A 11. B 16. C
2. D 7. B 12. C 17. C
3. A 8. A 13. D 18. B
4. A 9. C 14. A 19. D
5. C 10. D 15. D 20. A
Your program should store these correct answers in a list. The program should read the student’s answers for each of the 20 questions from a text file and store the answers in another list. (Create your own text file to test the application.) After the student’s answers have been read from the file, the program should display a message indicating whether the student passed or failed the exam. (A student must correctly answer 15 of the 20 questions to pass the exam.) It should then display the total number of correctly answered questions, the total number of incorrectly answered questions, and a list showing the question numbers of the incorrectly answered questions.
canswer = []
sanswer = []
file_vari = open("canswer.txt","r")
for i in file_vari:
j = i.rstrip("\n")
canswer.append(j)
file_vari.close()
file_vari1 = open("sanswer.txt","r")
for i in file_vari1:
j = i.rstrip("\n")
sanswer.append(j)
file_vari1.close()
print(canswer)
print(sanswer)
print("student result is : ")
i = 0
j = 0
for k in range(len(canswer)):
if (canswer[k] == sanswer[k]):
i += 1
else:
j += 1
print("correct answer is ",i,"out of",len(canswer))
print("incorrect answer is ",j,"out of",len(canswer))
If you have downloaded the source code from this book’s companion Web site, you will find the following files in the Chapter 08 folder:
• GirlNames.txt—This file contains a list of the 200 most popular names given to girls
born in the United States from the year 2000 through 2009.
• BoyNames.txt—This file contains a list of the 200 most popular names given to boys
born in the United States from the year 2000 through 2009.
Write a program that reads the contents of the two files into two separate lists. The user should be able to enter a boy’s name, a girl’s name, or both, and the application will display messages indicating whether the names were among the most popular.
answer :-
girlList = []
boyList = []
file_vari = open("GirlNames.txt","r")
for i in file_vari:
j = i.rstrip("\n")
girlList.append(j)
file_vari.close()
file_vari1 = open("BoyNames.txt","r")
for i in file_vari1:
j = i.rstrip("\n")
boyList.append(j)
file_vari1.close()
print("Girl List = ",end ="")
print(girlList)
print()
print()
print("Boy List = ",end ="")
print(boyList)
girlName = input("Enter the girl name : ")
boyName = input("Enter the boy name : ")
if girlName != "":
if girlName in girlList:
print("the girl that you entered is valid in the list")
else:
print("the girl that you entered isnot valid in the list")
else:
print("you didnot enter the girl name")
if boyName != "":
if boyName in boyList:
print("the boy that you entered is valid in the list")
else:
print("the boy that you entered isnot valid in the list")
else:
print("you didnot enter the boy name")
8. Population Data
If you have downloaded the source code from this book’s companion Web site, you will find a file named USPopulation.txt in the Chapter 08 folder. The file contains the midyear population of the United States, in thousands, during the years 1950 through 1990. The first line in the file contains the population for 1950, the second line contains the population for 1951, and so forth. Write a program that reads the file’s contents into a list. The program should display the following data:
• The average annual change in population during the time period
• The year with the greatest increase in population during the time period
• The year with the smallest increase in population during the time period
answer :-
populaionList = []
file_vari = open("USPopulation.txt","r")
for i in file_vari:
j = i.rstrip("\n")
populaionList.append(j)
file_vari.close()
print("pupulation List(1950 - 1990) = ",end="")
print(populaionList)
total = 0
for element in populaionList:
convertElement = int(element)
total += convertElement
averagePopulation = total / len(populaionList)
print("average annual change in population(1950 -1990) : ",format(averagePopulation,',.2f'))
print("year with greatest increase in population(1950 -1990) : ",max(populaionList))
print("year with smallest increase in population(1950 -1990) : ",min(populaionList))
9. World Series Champions
If you have downloaded the source code from this book’s companion Web site, you will find a file named WorldSeriesWinners.txt in the Chapter 08 folder. This file contains a chronological list of the World Series winning teams from 1903 through 2009. (The first line in the file is the name of the team that won in 1903, and the last line is the name of the team that won in 2009. Note that the World Series was not played in 1904 or 1994.) Write a program that lets the user enter the name of a team and then displays the number of times that team has won the World Series in the time period from 1903 through 2009.
answer :-
file_vari = open("WorldSeriesWinners.txt","r")
for i in file_vari:
j = i.rstrip("\n")
winnersList.append(j)
file_vari.close()
print("Winners List(1903 - 1994) = ",end="")
print(winnersList)
print("finding how many time a team won during 1903 - 1994")
teamName = input("Enter the team name : ")
count = 0
for i in winnersList:
if teamName == i:
count += 1
print("team name :",teamName)
print("Nuber of time they won during 1903 - 2009 :",count)
i will post chapter 9 very soon..
Caesars Palace Casino & Hotel - Dr. Maryland
ReplyDeleteDiscover the 남양주 출장샵 most exciting gambling events in the world 수원 출장안마 at 하남 출장마사지 Dr. Maryland. Check 고양 출장마사지 out 천안 출장샵 our casino promotions and promotions. Visit us for promotions,