Preview

07 - Simple Calculations

 1. The following program seeks to ask the user to enter a number: (no. of pounds) and then give them the equivalent Japanese yen. In the code here, line 2 has an error. 'yen' should be 'pounds'
def convert_currency():
     yen=int(input("Enter No. of Pounds:"))
     yen=pounds*150
     print("Converted into yen, that would give you:",yen)

convert_currency()

  True

  False

 2. There is an error in the line below on line 4. Line 4 should be: minutes=60*convert
def convert_hours():
     
     hours=int(input("Enter no. of hours"))
     minutes=60*x
     print(minutes)
     
convert_hours()

  False

  True

 3. What needs to go on line 6 to calculate the average of the three test scores?
def find_average():
     test1=30
     test2=100
     test3=99
     #find average of the three test scores
     
     average=total/3
     print("The average mark is:",average)

find_average()

  test1=test2=test2=total

  total=30+100+9

  total=test1+test2+test3

  total=add up all the tests please

 4. How would you print the area of the shape on line 6?
def create_shape():
     import turtle
     width=int(input("Enter width:"))
     height=int(input("Enter height:"))
     print("You have created a shape of width:",width,"and height:",height)
     #what would you type here to calculate area?
create_shape()

  print area which is equal to width and height

  print(area=width*height)

  area=print(width*height)

  print("area is equal to:",width*height)

 5. What would you type on line 5 to check to see if the user's entered guess is equivalent to the computer's guess which is stored in the variable computer_guess (equal to 7)
def guess():
  guess=int(input("Enter a number between 1 and 10:"))
  print("You have guessed:",guess)
  computer_guess=7
  #YOUR CODE HERE??
    print("Well done!")
  else:
    print("You came close, but not quite sorry!")
  
guess()

  if guess=computer_guess:

  if computer_guess = True

  if guess==computer_guess:

  if user input ==computer_guess:

 6. The expression int(age) below implies that ...
age=input("Enter age:")
int(age)
print(age)

  the variable age is an integer = 0

  the variable age can no longer be used as an integer

  the variable age is converted to a string

  the variable age is converted to integer.

 7. Trace the logic to predict the output of the following:
def main():
	a=1
	b=2
	a,b=4,2
	print(a+b)

main()

  2

  6

  1

  3

 8. The MOD or % operator gives the remainder. So, 10 MOD 2 =0, and 10 MOD 8 = 2. Difficult question for a beginner: Analyse the program below and see if you explain what it is doing? Try to code it and see.
#This is a python program that tells you if a number is or is not a ....

num = 407

# take input from the user
# num = int(input("Enter a number: "))
if num > 1:
   # check for factors
   for i in range(2,num):
       if (num % i) == 0:
           print(num,"is not.....")
           print(i,"times",num//i,"is",num)
           break
   else:
       print(num,"is one!")
       
else:
   print(num,"is not")

  a number that is divisible by 2

  a number that has a remainder of 0

  a prime number

  a number that is in the range of 2 + num

 9. This is another program that uses MOD (%). Fill in the blanks on line 4 and 6. The program asks for a number and what does it calculate?
#Code this yourself and see what it does:
num = int(input("Enter a number: "))
if (num % 2) == 0:
   print("The number is _________")
else:
   print("The number is _________")

  Line 4: Is PRIME Line 6: Is not PRIME

  Line 4: Gives a remainder of 2 Line 6: Does not give a remainder of 2

  Line 4: Is a multiple of 2 Line 6: Is not a multiple of 2

  Line 4: EVEN Line 6: ODD

 10. If the number entered was: 7, animal was 'cat' and first name was 'Bob', the username would be?
def main():
  number=input("Enter your favourite number:")
  animal=input("Enter your favourite animal")
  firstname=input("Enter your firstname")
  username=number+firstname+animal
  print(username)

main()

  Bob7Cat

  CatBob7

  7BobCat

  77