Preview

12 - Solving Mini Problems

 1. The following code needs to convert hours into minutes but there is an error. How would you fix it?
def convert_hours():
     #There is an error in the code below. Can you fix it? The code needs to convert hours into minutes
     hours=int(input("Enter no. of hours"))
     minutes=60*x
     print(minutes)
     
convert_hours()

  Change line 4 to: x * 60 minutes

  Change line 4 to: 60 x 60 x 60 = minutes

  Change line 4 to: minutes=60*hours

  Change line 4 to: minutes = 60

 2. Can you predict/describe what this function is doing?
def calculate():
     #Add two more tests scores for test4 and test5.
     #Adapt the code to find the average for the five test scores
     test1=30
     test2=100
     test3=99
     #find average of the three test scores
     total=test1+test2+test3
     calculate=total/3
     print("The average mark is:",calculate)

calculate()

  adding together three test scores

  adding together three test scores and then finding a percentage

  finding the average of three test scores

  calculating the highest test score from three test scores that are input into the system

 3. What would you add on line 8 to print a message and the number of remaining friends
def friends_list():
     #Add the facility to also add more friends after the remove function.
     #After removing x amount of friends (that the user specifies), print the remaining no. of friends again
     friends=int(input("Enter No. of Friends"))
     print("Thank you, you have,",friends,"friends")
     remove=int(input("Enter No. of Friends you wish to remove:"))
     remaining=friends-remove
     #hat would you type on this line to print the remaining friends
     friends_list()
                     

friends_list()

  message("You have this many friends remaining") + remaining (print)

  print("You have this many friends: remaining:")

  print("You have this many friends remaining:",remaining)

  All of the above are valid options

 4. There may be something missing from this program that calculates the area of a circle, given its radius. What is it?
from math import pi
r = input ("Input the radius of the circle : ")
print ("The area of the circle with radius " + str(r) + " is: " + str(pi * r**2))

  An additional line needs to be added to give the variable 'pi' a value of 3.14

  Line 2 should be: r = float(input ("Input the radius of the circle : "))

  Line 2 should be: r = input(radius("Input the radius of the circle:"))

  There is nothing missing. An input of 3.4 (for radius) would accurately give the area

 5. Which statement is most true in reference to the following code?
def findspeed():
  distance=int(input("Enter distance:"))
  time=int(input("Enter time: [for the sake of simplicity enter a whole number]>>"))
  speed=distance/time
  print("The speed of the runner is:",speed)

findspeed()

  Distance = 100, Time = 2,will give a result of 50.0

  All of the above are valid statements in reference to the given code

  The instructions to enter distance and time could be validated and clearer

  Speed is in metres per second or miles per hour etc, and this is not included

 6. If the user enters 3, what will be ouput or printed to the screen?
def main():
  num=int(input("Enter a number between 5 and 50:"))
  x=0
  while x<num+1:
    print(x)
    x=x+1


main()

  5,10,15

  1,2,3

  0,1,2,3

  A list of numbers from 5 to 50

 7. What is missing from line 6?
def count_to_twenty():
  print("-----Counting to 20 using a while loop-------------")
  x=0
  while x<21:
    print(x)
    #What is missing here?
    
main()
count_to_twenty()

  x > 1

  x = x + 1

  x = 20

  x + x - 1

 8. In the following program, if the user enters 5, it only prints: 0,1,2,3,4. How can it print all the way up to 5?
num=int(input("Enter integer:"))
for i in range(0,num):
  print(i)

  Change line 2 to: for i in range(5,0)

  All of the above are valid options

  Change line 2 to: for i in range(0,5):

  Change line 2 to: for i in range(0,num+1):

 9. What would the output be if the input here was: favourite number: 2; animal: dog, firstname: Joe
def main():
  number=input("Enter your favourite number:") #note you do not need to convert number here to an integer as you don't need to perform any calculations with it, but rather treat it like a string!
  animal=input("Enter your favourite animal")
  firstname=input("Enter your firstname")
  username=number+firstname+animal
  print(username)

main()

  2JD

  JoeDog2

  DogJoe2

  2JoeDog

 10. What will the output of the following code be?
def main():
   score=500 #initialise the score variable to 500
   while score<504:
       print("Whoo, you're in a loop") #print this while above condition true
       score=score+1 #the score keeps going up for each loop round
main()

  "Whoo, you're in a loop" x 500 + 504 times

  "Whoo, you're in a loop" x 500 times

  "Whoo, you're in a loop" x 504 times

  "Whoo, you're in a loop" x 4 times