Advertisement
Mars83

4-7

Oct 4th, 2011
293
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.78 KB | None | 0 0
  1. #! /usr/bin/env python3.2
  2. # -*- coding: utf-8 -*-
  3.  
  4. # main.py
  5. """ Task: Exercise 4.7:
  6.    Rewrite the grade program from the previous chapter using a function called
  7.    computegrade that takes a score as its parameter and returns a grade as a
  8.    string.
  9.    Score Grade
  10.    > 0.9 A
  11.    > 0.8 B
  12.    > 0.7 C
  13.    > 0.6 D
  14.    <= 0.6 F
  15.    Program Execution:
  16.    Enter score: 0.95
  17.    A
  18.    Enter score: perfect
  19.    Bad score
  20.    Enter score: 10.0
  21.    Bad score
  22.    Enter score: 0.75
  23.    C
  24.    Enter score: 0.5
  25.    F
  26.    Run the program repeatedly to test the various different values for input.
  27. """
  28.  
  29. # Functions
  30. def determine_grade(score):
  31.     """
  32.    Determines the grade of a given score (string or float/int)
  33.    Returns grade
  34.    """
  35.     grade = None
  36.     try:
  37.         score = float(score)
  38.         if score < 0.0 or score > 1.0:
  39.             raise 1         # simulates an error
  40.         elif score > 0.9:
  41.             grade = "A"
  42.         elif score > 0.8:
  43.             grade = "B"
  44.         elif score > 0.7:
  45.             grade = "C"
  46.         elif score > 0.6:
  47.             grade = "D"
  48.         elif score >= 0:
  49.             grade = "F"
  50.         else:
  51.             raise 1         # simulates an error
  52.     except:
  53.         grade = "Bad score"
  54.    
  55.     return grade
  56.        
  57. # Main
  58. loop = True
  59. while loop:
  60.     score = None
  61.     grade = None
  62.     # Enter score
  63.     while score == None:
  64.         try:
  65.             score = float(input("Enter score: "))
  66.         except ValueError:
  67.             score = None
  68.             print("Invalid value!")
  69.             continue
  70.         except:
  71.             score = None
  72.             print("Error")
  73.             continue
  74.         if score < 0 or score > 1:
  75.             score = None
  76.             print("Please enter a value between 0.0 and 1.0!")
  77.             continue
  78.     grade = determine_grade(score)
  79.     print("The grade of " + str(score) + " is " + str(grade) + ".\n")
  80.     print("Do you want to enter a new score?")
  81.     try:
  82.         choice = input("(Y/N): ")
  83.     except:
  84.         choice = 'N'
  85.     if choice.upper() == 'Y' or choice.upper() == 'YES':
  86.         # 'y', 'Y', 'yes', 'yEs', ... 'YES' are valid
  87.         continue
  88.     else:                       # everything else is invalid
  89.         loop = False            # and stops the program
  90.        
  91. # test
  92. print("\nTesting string parameter in determine_grade(score):")
  93. print("Score: 0.815\t\tResult: " + str(determine_grade("0.815")))
  94. print("Score: 1.337\t\tResult: " + str(determine_grade("1.337")))
  95. print("Score: lagerregal\tResult: " + str(determine_grade("lagerregal")))
  96. print("Score: -0.02\t\tResult: " + str(determine_grade("-0.02")))
  97. print("Score: 0.699999999\tResult: " + str(determine_grade("0.699999999")))
  98. print("Score: 0.700000001\tResult: " + str(determine_grade("0.700000001")))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement