In: Computer Science
in PYTHON
If varible counter contains an integer, then write an augmented assignment operation to decrement it by 1. A “count-controlled” for loop using the xrange function to average the scores for a class is given below and in the file PartD_average_with_for.py:
# Calculates the average score using a counter-controlled for-loop numberOfScores = input("Enter the number of scores to average: ")
total = 0.0;
for counter in xrange(numberOfScores):
score = input("Enter score #" + str(counter+1) + ": ") total = total + score
if numberOfScores > 0:
print "The average score is", total / numberOfScores
Code:
#Define count
numberOfScores=input("Enter count of scores:")
#Define total
total = 0.0;
#Loop until count of scores
for counter in xrange(numberOfScores):
#Get value of score
score = input("Enter score #" + str(counter+1) + ": ")
#Update score
total = total + score
#Decrement counter
counter = counter-1
#If count greater than 0
if numberOfScores > 0:
#Display result
print "The average score is", total / numberOfScores
#Receive number of days
days = int(input("Number of days"))
#Set amount as 1
amount = 1
#Loop until days count
for lcounter in range(days):
#Double amount each day
amount = amount*2
#Display total salary paid
print"Total salary paid is "
print amount
print "pennies"