Deck 1 - Higher Computing - Programming Tasks Flashcards

You may prefer our related Brainscape-certified flashcards:
1
Q

Create a program that does the following:

  • Asks the user for the number of people in a class.
  • Asks the user for the names of those people.
  • Records those names in a file called ‘names.txt’
A
def getNumInClass():
    numInClass = input("Enter the number of people in the class: ")
    return numInClass
def getNamesInClass(numInClass):
    names = []
    for i in range(int(numInClass)):
        names.append(input("Enter the name of a student: "))
    return names
def saveToFile(names):
    file = open("names.txt", "w")
    file.write(str(names))

numInClass = getNumInClass()
names = getNamesInClass(numInClass)
saveToFile(names)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

Create a program that does the following:

  • Stores 5 people’s high scores from 3 games: Candy Crush, Lemmings and Donkey Kong.
  • The data will come from the file Gaming.
  • The program should then calculate the average scores for each of the games and display the answers to the user.
(Copy and paste into a txt file:
James,Jamieson,134500,644821,41041
Sally,Smith,193433,30443,19339
Biff,Bacon,353335,74564,54673
David,Davidson,47764,34332,13214
Zander,Zouch,31115,243511,23251
)
A
class Game:
 def \_\_init\_\_(self):
   self.firstName = ""
   self.lastName = ""
   self.candy = 0
   self.lemming = 0
   self.donkey = 0
def getStuff():
  gamingStuff = open('gaming.txt')

highscores = []

  for line in gamingStuff:
   newGame = Game()
   data = line.split(",")
   newGame.firstName = data[0]
   newGame.lastName = data[1]
   newGame.candy = int(data[2])
   newGame.lemming = int(data[3])
   newGame.donkey = int(data[4])

highscores.append(newGame)
gamingStuff.close()
return highscores

def calcAverage(highscores):
  totalCandy = 0
  totalLemming = 0
  totalDonkey = 0
  for i in range(5):
    totalCandy = totalCandy + highscores[i].candy 
    totalLemming = totalLemming + highscores[i].lemming 
    totalDonkey = totalDonkey + highscores[i].donkey
  averageCandy = totalCandy / 5
  averageLemming = totalLemming / 5 
  averageDonkey =  totalDonkey / 5
  return averageCandy, averageLemming, averageDonkey

highscores = getStuff()
averageCandy, averageLemming, averageDonkey = calcAverage(highscores)
print(‘The average score for Candy Crush was ‘ + str(averageCandy))
print(‘The average score for Lemmings was ‘ + str(averageLemming))
print(‘The average score for Donkey Kong was ‘ + str(averageDonkey))

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

Create a program that does the following:

  • Creates an array of a certain number of random numbers from 1 to 10, the amount chosen by the user
  • Find the maximum and minimum number in the array
  • Takes a number from the user and displays the amount of that number in the array
A

import random

def getNumbers():
  arrayAmount = input("Enter the amount of numbers you want: ")
  numArray = []
  for i in range(arrayAmount):
    numArray.append(random.randint(1,10))
  return numArray
def maxAndMinNum(numArray):
  maxNum = numArray[0]
  minNum = numArray[0]
  for i in range(len(numArray)):
    if numArray[i] > maxNum:
      maxNum = numArray[i]
  for i in range(len(numArray)):
    if numArray[i] < minNum:
      minNum = numArray[i]
  print(maxNum + " is the max num")
  print(minNum + " is the min num")

def countNum(numArray):
number = input(“Enter the number you want to count”)
counter = 0
for i in range(len(numArray)):
if numArray[i] == number:
counter += 1
print(“There are “ + counter + “ of them”)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly