Python Forum
I keep getting errors and I'm lost!
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
I keep getting errors and I'm lost!
#1
Hello all! And thanks for taking the time to view this thread. THIS IS A HOMEWORK ASSIGNMENT. Just so y'all know I'm pretty new at this, and I want to master it. So this is my assignment:

Rewrite the grades with functions program from the previous chapter making sure you maintain your computegrade function that takes a score as its parameter and returns a grade as a string. Update that program if needed based on the feedback I gave you in D2L to make sure you have both a parameter and a return value.

Add the ability for the user to run this program over and over until they type some sort of terminal statment such as 'quit.' You may use your own creativity with your input and output statements, but be sure you are communicating what is expected to your user.

Don't forget your comments including name/date/purpose and inline comments.

Sample input/output:

Score Grade
>= 0.9 A
>= 0.8 B
>= 0.7 C
>= 0.6 D
< 0.6 F

Enter score or type 'Quit' to exit: 0.95
A
Enter score or type 'Quit' to exit: perfect
Bad score
Enter score or type 'Quit' to exit: 10.0
Bad score
Enter score or type 'Quit' to exit: 0.75
C
Enter score or type 'Quit' to exit: 0.5
F
Enter score or type 'Quit' to exit: Quit
Thanks for using the grade calculation program!

Run the program repeatedly to test the various different values for input.

Basically we are learning about while loops and adding them to previous concepts we've learned.
Here is my code:
# Samuel Snelling Assignment 9 10/21/18
# if letters are entered instead of numbers, 
#program will send "Bad Score" to user and 
# ask user to "Enter score or type 'Quit' to exit".
try:
  score = input("Enter score or type 'Quit' to exit: ")
except:
  print("Bad score.")
  score = input("Enter score or type 'Quit' to exit: ")


def computegrade(score):
    
  while True:
# if a number is entered that is larger than 1.0, program will 
# send "Bad Score" to user and ask the user "Enter score or type 
# 'Quit' to end program".
    if float(score) > 1.0:
      print("Bad score.")
      score = input("Enter score or type 'Quit' to exit: ")
      
# if number entered is equal to 1.0 or in between 1.0 and 0.9 it 
# will print "A"
    elif float(score) == 1.0 or float(score) >= 0.9:
      grade = ("A")
      print("A")
      score = input("Enter score or type 'Quit' to exit: ")

# if number entered is equal to 0.89 or in between 0.89 and 0.8 it 
# will print "B"
    elif float(score) == 0.89 or float(score) >= 0.8:
      grade = ("B")
      print("B")
      score = input("Enter score or type 'Quit' to exit: ")

# if number entered is equal to 0.79 or in between 0.79 and 0.7 it 
# will print "C"
    elif float(score) == 0.79 or float(score) >= 0.7 :
      grade = ("C")
      print("C")
      score = input("Enter score or type 'Quit' to exit: ")
  
# if number entered is equal to 0.69 or in between 0.69 and 0.6 it
# will print "D"
    elif float(score) == 0.69 or float(score) >= 0.6:
      grade = ("D")
      print("D")
      score = input("Enter score or type 'Quit' to exit: ")

# if number entered is less than 0.6 it will print "F"
    elif float(score) < 0.6:
      grade = ("F")
      print("F")
      score = input("Enter score or type 'Quit' to exit: ")
# if "Quit" is entered by user, 
# "Thanks for using the grade calculation program!" will print and 
# end the program.  
    if score == "Quit":
      print("Thanks for using the grade calculation program!")
      quit()
    return grade
print(computegrade(score))
Here is an error I keep getting:
Traceback (most recent call last):
File "/tmp/sessions/eaf64abee3b59bbe/main.py", line 62, in <module>
print(computegrade(score))
File "/tmp/sessions/eaf64abee3b59bbe/main.py", line 18, in computegrade
if float(score) > 1.0:
ValueError: could not convert string to float: 'Quit'

Also, when I type in numbers between 1.0 and 0.0, I will get the correct grade for the first number, but not for the second number.
Example:
Enter score or type 'Quit' to exit: .9
A
Enter score or type 'Quit' to exit: .3
A

I seriously don't know what to do to fix these things, and I'm starting to think the wall would look nice with a computer sticking out of it. I hope this is enough information. And I understand y'all are not here to do my homework. I don't want y'all to, I just need help.
Reply
#2
Okay, you're receiving the error because of line 18. The last few lines of the traceback indicate that "if float(score) > 1.0" is generating the problem. Moreover, the problem is that "Quit" cannot be converted to a float. To fix this, you need to check for the entry "Quit" prior to calling float() on the score. Otherwise, you need to use a try...except block to catch the error and then check for "Quit".

As for your second problem, the return is currently inside your while loop. So, at the end of the first iteration, your function is returning the grade that it evaluated. It is returning prior to the second entry being evaluated.

For a critique, you have a lot of repetition in your code. Line 20, for instance, is repeated eight times. To improve your loop, try to refactor it to only have that line appear once.
Reply
#3
Your while loop should not be in the function. It should be where you ask for input. So you keep looping, asking for input each time. Once you get the input, you need to check it for three things. If it is 'Quit', you need to exit the loop. If it is a valid float, you should send it to computer_grade. Otherwise, you reply with 'Bad input.'.

You sort of have that with the try/except block. Here's a way you might use it: First, get the input. Then, in the try block, send it to computer_grade. If that raises an error (because of an invalid float) it goes to the except block. In the except block have an if/else statement. If the input is quit, exit the loop; otherwise print 'bad input'.

The reason you are getting the error is that you are not checking that you have a valid float before you send it to computer_score. When you enter correct values, you are only getting to enter two because your loop is in the wrong place. You ask once at the top, then you ask once in the computer_grade function, then you return on line 61, breaking out of the loop. The computer_grade function should do nothing but translate the numeric grade into a letter grade. Take all of the looping and inputting out of there.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#4
Wow! Thank you guys for your help! Unfortunately, I am running into another problem. When I type in letters other than "Quit" it is suppose to say "Bad Score" but I am getting this error:

Traceback (most recent call last):
File "/tmp/sessions/375e0408eaad1508/main.py", line 23, in <module>
if float(score) > 1.0:
ValueError: could not convert string to float: 'perfect'

I thought the try and except was suppose to take care of that...

Here is my new code so far:

# if letters are entered instead of numbers, 
#program will send "Bad Score" to user and 
# ask user to "Enter score or type 'Quit' to exit".
try:
  score = input("Enter score or type 'Quit' to exit: ")
except:
  print("Bad score.")


def computegrade(score):
    return grade
while True:
# if "Quit" is entered by user, 
# "Thanks for using the grade calculation program!" will print and 
# end the program.  
  if score == "Quit":
      print("Thanks for using the grade calculation program!")
      quit()
# if a number is entered that is larger than 1.0, program will 
# send "Bad Score" to user and ask the user "Enter score or type 
# 'Quit' to end program".
  if float(score) > 1.0:
      print("Bad score.")
      score = input("Enter score or type 'Quit' to exit: ")
      
# if number entered is equal to 1.0 or in between 1.0 and 0.9 it 
# will print "A"
  elif float(score) == 1.0 or float(score) >= 0.9:
      grade = ("A")
      print("A")
      score = input("Enter score or type 'Quit' to exit: ")

# if number entered is equal to 0.89 or in between 0.89 and 0.8 it 
# will print "B"
  elif float(score) == 0.89 or float(score) >= 0.8:
      grade = ("B")
      print("B")
      score = input("Enter score or type 'Quit' to exit: ")

# if number entered is equal to 0.79 or in between 0.79 and 0.7 it 
# will print "C"
  elif float(score) == 0.79 or float(score) >= 0.7 :
      grade = ("C")
      print("C")
      score = input("Enter score or type 'Quit' to exit: ")
  
# if number entered is equal to 0.69 or in between 0.69 and 0.6 it
# will print "D"
  elif float(score) == 0.69 or float(score) >= 0.6:
      grade = ("D")
      print("D")
      score = input("Enter score or type 'Quit' to exit: ")

# if number entered is less than 0.6 it will print "F"
  elif float(score) < 0.6:
      grade = ("F")
      print("F")
      score = input("Enter score or type 'Quit' to exit: ")
# if "Quit" is entered by user, 
# "Thanks for using the grade calculation program!" will print and 
# end the program.  
  if score == "Quit":
      print("Thanks for using the grade calculation program!")
      quit()
  
print(computegrade(score))
Reply
#5
The try...except block would take care of that if implemented correctly. As is, there are two problems:

1. The try...except block is outside the loop meaning that it would only be used for the first input only.
2. The function used in the try block must raise an error for the except block to be called up. Input() is not going to raise an error. Float() would raise an error if the inputted value could not be converted to a float.
Reply
#6
You have been awesome! Thank you again for the tips! I'm so close! The only thing that is happening now is that when I type in Quit it says "Bad Score".

Here is my new code:
def computegrade(score):
    return grade

while True:
# if letters are entered instead of numbers, 
#program will send "Bad Score" to user and 
# ask user to "Enter score or type 'Quit' to exit".
  try:
    score = float(input("Enter score or type 'Quit' to exit: "))
  except:
    print("Bad score.")
    
    continue
# if "Quit" is entered by user, 
# "Thanks for using the grade calculation program!" will print and 
# end the program.  
  if float(score) == "Quit":
    print("Thanks for using the grade calculation program!")
    quit()

# if a number is entered that is larger than 1.0, program will 
# send "Bad Score" to user and ask the user "Enter score or type 
# 'Quit' to end program".    
  elif float(score) > 1.0:
      print("Bad score.")
      
      
# if number entered is equal to 1.0 or in between 1.0 and 0.9 it 
# will print "A"
  elif float(score) == 1.0 or float(score) >= 0.9:
      grade = ("A")
      print("A")
      

# if number entered is equal to 0.89 or in between 0.89 and 0.8 it 
# will print "B"
  elif float(score) == 0.89 or float(score) >= 0.8:
      grade = ("B")
      print("B")
      

# if number entered is equal to 0.79 or in between 0.79 and 0.7 it 
# will print "C"
  elif float(score) == 0.79 or float(score) >= 0.7 :
      grade = ("C")
      print("C")
      
  
# if number entered is equal to 0.69 or in between 0.69 and 0.6 it
# will print "D"
  elif float(score) == 0.69 or float(score) >= 0.6:
      grade = ("D")
      print("D")
      

# if number entered is less than 0.6 it will print "F"
  elif float(score) < 0.6:
      grade = ("F")
      print("F")
      
# if "Quit" is entered by user, 
# "Thanks for using the grade calculation program!" will print and 
# end the program.  
  if score == "Quit":
      print("Thanks for using the grade calculation program!")
      quit()
  
print(computegrade(score))
Reply
#7
You're getting closer. The code is steadily improving. Here are the most pressing, current issues to address:

1. As written, your code will continue the loop if a non-numeric entry has been entered. Continuing skips the rest of the loop body and starts the next iteration of the loop. If you enter "Quit", it will raise an exception, print "Bad Score", and start over without checking the conditional on line 17. To fix this, you can simply move that conditional into the except block of your try...except.

2. Computegrade() isn't doing anything right now. Your loop is currently infinite because of problem #1 and the interpreter is never executing line 68. Once #1 is corrected, you will receive a new error. What Ichabod was stating earlier relates to the Single Responsibility Principal (SRP; or atomicity). A function should do only one thing. Since computegrade() is all about determining the letter grade of a giving decimal, your entire if statement from lines 30 through 59 should be inside of it (remember to update the keywords when you move it all).

3. Line 68 should be inside the loop. Repeating that line will perform all the calculations for the grade because it's calling computegrade() (once #2 is corrected). Once this is corrected, you'll start seeing the letter grade print twice. This will be due to the print() calls inside of computegrade(), which you don't need.
Reply
#8
After doing step 2 my code is not producing anything :( idk what I'm doing wrong.

def computegrade(score):
    
  while True:
# if letters are entered instead of numbers, 
#program will send "Bad Score" to user and 
# ask user to "Enter score or type 'Quit' to exit".
    try:
      score = float(input("Enter score or type 'Quit' to exit: "))
    except:
      print("Bad score.")
# if "Quit" is entered by user, 
# "Thanks for using the grade calculation program!" will print and 
# end the program.  
      if float(score) == "Quit":
        print("Thanks for using the grade calculation program!")
        quit()     
        continue

 
# if a number is entered that is larger than 1.0, program will 
# send "Bad Score" to user and ask the user "Enter score or type 
# 'Quit' to end program".    
      if float(score) > 1.0:
        print("Bad score.")
       
       
# if number entered is equal to 1.0 or in between 1.0 and 0.9 it 
# will print "A"
      elif float(score) == 1.0 or float(score) >= 0.9:
        grade = ("A")
        print("A")
       
 
# if number entered is equal to 0.89 or in between 0.89 and 0.8 it 
# will print "B"
      elif float(score) == 0.89 or float(score) >= 0.8:
        grade = ("B")
        print("B")
       
 
# if number entered is equal to 0.79 or in between 0.79 and 0.7 it 
# will print "C"
      elif float(score) == 0.79 or float(score) >= 0.7 :
        grade = ("C")
        print("C")
       
   
# if number entered is equal to 0.69 or in between 0.69 and 0.6 it
# will print "D"
      elif float(score) == 0.69 or float(score) >= 0.6:
        grade = ("D")
        print("D")
       
 
# if number entered is less than 0.6 it will print "F"
      elif float(score) < 0.6:
        grade = ("F")
        print("F")
    return grade
    print(computegrade(score))
Reply
#9
There's been some miscommunication. You already have all the code you need written; we just need to reorganize to get it in working order. So, I'm going to give you a layout to fill in to help you along:

"""computegrade should take a float named score and return a letter grade.
No loop inside.
"""
def computegrades(score):
    pass

"""main should contain your loop, your input, input verification, and a call
to computegrade once the input has been turned into a float.
"""
def main():
    #start loop
        #input

        #verify input, terminate if "quit"

        #call computegrade
    pass

# call main to start program
main()
Reply
#10
Ok, I will try that. Thank you for all your help so far!
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  I am completely lost on this homework assignment a36 1 1,747 Feb-21-2022, 06:01 AM
Last Post: buran
  Very Lost vollynez 3 1,860 Oct-02-2020, 09:09 PM
Last Post: Yoriz
  I got lost in the program Gadev 1 1,882 Nov-11-2018, 05:50 PM
Last Post: j.crater
  test..and I'm already lost sannixinc 1 2,501 Feb-22-2018, 09:09 PM
Last Post: glidecode
  Completly lost about to fail my class! crakacheezy 7 4,528 Dec-28-2017, 11:50 PM
Last Post: mepyyeti

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020