Home > Blockchain >  I am writing a simple code to ask someone's name, their teacher's name and then average ou
I am writing a simple code to ask someone's name, their teacher's name and then average ou

Time:10-04

def user_name():
  ask_name = str(input("Enter your name: "))
  return ask_name

def print_greeting(ask_name):
  str(print("Your name is {0}.".format(ask_name)))

ask_name = user_name()
print_greeting(ask_name)

def teacher_name():
  ask_teacher_name = str(input("What is the name of your computer science teacher?: "))
  return ask_teacher_name

def print_teacher_greeting(ask_teacher_name):
  str(print("Your computer science teacher's name is {0}.".format(ask_teacher_name)))

ask_teacher_name = teacher_name()
print_teacher_greeting(ask_teacher_name)

Not sure but I think the above lines are fine and don't have anything to do with the problem but it is something to do with how I've phrased the determine_outcome function.

def calc_average( score1, score2, score3, score4, ):
  average = ( score1   score2   score3   score4 ) / 4
  return average

def ask_score():
  score1 = float(input( "Please enter score 1: "))
  score2 = float(input( "Please enter score 2: "))
  score3 = float(input( "Please enter score 3: "))
  score4 = float(input( "Please enter score 4: "))

  return score1, score2, score3, score4

def determine_outcome(user_score):
  if(user_score <=8 ):
    print("Well done {0}, your average score was {1}. {2} would be proud of you.".format(str(print_greeting), str(ask_score), str(print_teacher_greeting)))
  elif(user_score >= 6 < 8):
    print("A good effort, {0}, your average was {1}. {2} thinks you should check your work more carefully.".format(str(print_greeting), str(ask_score), str(print_teacher_greeting)))
  elif(user_score <=5 ):
    print("{0}, this is quite poor. Your average was only {1}. {2} has asked you to try harder please.".format(str(print_greeting), str(ask_score), str(print_teacher_greeting)))

def main():
  score1, score2, score3, score4 = ask_score()
  determine_outcome(ask_score)

main()

#if(user_score <=8 ):
#TypeError: '<=' not supported between instances of 'function' and 'int'

This is always the result and I don't know where I have gone wrong I am quite new to coding so it might be an obvious mistake but I can't see anything wrong at the moment.

CodePudding user response:

Change the main function from

def main():
  score1, score2, score3, score4 = ask_score()
  determine_outcome(ask_score)

to

def main():
  for score in ask_score():
    determine_outcome(score)

as the way the ask_score function is defined, it returns an iterable (a tuple) of floats, allowing us to use a for loop to pass each float into the determine_outcome function individually.


Just realized that the calc_average function was never called in your code. I believe that the main function was supposed to be

def main():
  user_score = calc_average(*ask_score())
  determine_outcome(user_score)

CodePudding user response:

Assuming that you want to pass the average score of the student to the determine_outcome() function in main(), your main() should be as follows:

def main():
  score1, score2, score3, score4 = ask_score()
  determine_outcome(calc_average(score1, score2, score3, score4))

Also you have to change your determine_outcome() function as it does not recognize ask_score. Change it to user_score and it would work.

You also have to change the print_greeting() and print_teacher_greeting() function and return the str:

def print_greeting(ask_name):
  return str("Your name is {0}.".format(ask_name))

def print_teacher_greeting(ask_teacher_name):
  return str("Your computer science teacher's name is {0}.".format(ask_teacher_name))

I would personally suggest you to use f-string:

def print_greeting(ask_name):
  return f"Your name is {ask_name}."

def print_teacher_greeting(ask_teacher_name):
  return f"Your computer science teacher's name is {ask_teacher_name}."

Editing my answer further, change the function as given below:

def determine_outcome(user_score):
  if(user_score <=8 ):
    print("Well done {0}, your average score was {1}. {2} would be proud of you.".format(print_greeting(ask_name), str(user_score), print_teacher_greeting(ask_teacher_name)))
  elif(user_score >= 6 < 8):
    print("A good effort, {0}, your average was {1}. {2} thinks you should check your work more carefully.".format(print_greeting(ask_name), str(user_score), print_teacher_greeting(ask_teacher_name)))
  elif(user_score <=5 ):
    print("{0}, this is quite poor. Your average was only {1}. {2} has asked you to try harder please.".format(print_greeting(ask_name), str(user_score), print_teacher_greeting(ask_teacher_name)))
  • Related