In: Computer Science
HIMT 345
Homework 05: Functions
Overview: Examine PyCharm’s “Introduction to Python”
samples for Functions. Use PyCharm to work along with exercises
from Chapter 4. Modify the grade assignment program of Hwk 04 to
utilize a function by copying the Hwk04 project and creating the
Hwk05 project.
Prior Task Completion:
1. Read Chapter 04
2. View Chapter 04’s video notes.
3. As you view the video, work along with each code sample in
PyCharm. The examples used in the video are available for you to
experiment with if you downloaded the complete Severance source
files for Chapters 3-10.
a) With PyCharm open, locate the .zip file entitled Ch 4
Functions PPT Code Samples.zip.
b) Open each of the project files for that chapter in PyCharm.
Note: The code samples are numbered in the order they are covered
in the video.
c) Start the video and follow along.
4. Complete Exercises 4.1 – 4.5 (not handed in).
Specifics: PyCharm’s “Introduction to Python” project contains
multiple examples giving the basics of conditional expressions (see
list at right). Follow the instructions to complete them. (Not
handed in.)
Use PyCharm to work along with the video solution for Exercise 4.6
from the textbook. Try to make the same mistakes (and fixes) made
by the author. (Not handed in.)
Create a copy of your Hwk04 project file, calling it
Hwk05.
1. Highlight the Hwk04 project, select
Refactor/Copy.
Important: Do not skip the process of following along with
the videos. It is a very important part of the learning
process!
2. Give it a New name: of Hwk05 and leave the To directory:
in its default state.
Leave the “Open copy in editor” box checked.
3. After clicking OK, if the project fails to open, just open it
manually as you would
any project (File | Open | Hwk05 project).
You may immediately delete Hwk04b as it is not needed in this
assignment. You may
need to Refactor | Rename both the project name and the existing
Hwk04a.py file, as
only the directory was renamed by PyCharm in creating the
copy.
Having renamed Hwk04a to Hwk05_YourLastName.py, your task is to
determine the
letter grade using a function.
Here is some pseudocode* to guide the process:
(NOTE: The assign_grade function must be declared at the top of the
Python file.)
Prompt for the test score, using try-except to verify it is
valid
Invoke (call) the function, supplying it the test score and it
returning the appropriate
letter grade;
e.g. letter_grade = assign_grade(test_score)
Display the test score and appropriate letter grade.
What to hand in:
Take screen shots of the console window verifying correct
handling of bad input as well
as letter grades for one test score in each grade range. Copy the
screen shots into a
Word document (filename Hwk05_YourLastName_Screenshots.doc) with
your name and
date in the upper right hand corner. Upload the Word doc and the
Python program file
(Hwk05_YourLastName.py) to the appropriate assignment in the
LMS.
NOTE: As was described in the previous assignment, each Python file
you create should
be documented with (minimally) the following three lines at
the top of each file:
# Filename: Hwk03.py
# Author:
# Date Created: 2016/09/05
# Purpose:
(*) pseudocode: a notation resembling a simplified programming
language, used in
program design.
HERE IS HOMEWORK4
# main function def main(): test_score = int(input('Enter Your Test Score in the range (0-100) inclusively: ')) # for incorrect input if test_score<0 or test_score>100: print('Sorry, Your Input Score is not in the given Range!!! Try another time!!!') else: # find grade if test_score>=90: print("Your Grade:", 'A') elif test_score>=80: print("Your Grade:", 'B') elif test_score>=70: print("Your Grade:", 'C') elif test_score>=60: print("Your Grade:", 'D') else: print("Your Grade:", 'F') # execution of program starts from here if __name__ == '__main__': main()
OUTPUT
(base) avianjjai@avianjjai-Vostro-3578:~/Desktop/Chegg$ python chegg100.py Enter Your Test Score in the range (0-100) inclusively: -8 Sorry, Your Input Score is not in the given Range!!! Try another time!!! (base) avianjjai@avianjjai-Vostro-3578:~/Desktop/Chegg$ python chegg100.py Enter Your Test Score in the range (0-100) inclusively: 105 Sorry, Your Input Score is not in the given Range!!! Try another time!!! (base) avianjjai@avianjjai-Vostro-3578:~/Desktop/Chegg$ python chegg100.py Enter Your Test Score in the range (0-100) inclusively: 54 Your Grade: F (base) avianjjai@avianjjai-Vostro-3578:~/Desktop/Chegg$ python chegg100.py Enter Your Test Score in the range (0-100) inclusively: 64 Your Grade: D (base) avianjjai@avianjjai-Vostro-3578:~/Desktop/Chegg$ python chegg100.py Enter Your Test Score in the range (0-100) inclusively: 75 Your Grade: C (base) avianjjai@avianjjai-Vostro-3578:~/Desktop/Chegg$ python chegg100.py Enter Your Test Score in the range (0-100) inclusively: 85 Your Grade: B (base) avianjjai@avianjjai-Vostro-3578:~/Desktop/Chegg$ python chegg100.py Enter Your Test Score in the range (0-100) inclusively: 95 Your Grade: A (base) avianjjai@avianjjai-Vostro-3578:~/Desktop/Chegg$ python chegg100.py Enter Your Test Score in the range (0-100) inclusively: 100 Your Grade: A (base) avianjjai@avianjjai-Vostro-3578:~/Desktop/Chegg$ python chegg100.py Enter Your Test Score in the range (0-100) inclusively: 0 Your Grade: F (base) avianjjai@avianjjai-Vostro-3578:~/Desktop/Chegg$
# main() function def main(): # prompt first name first_name = input('First name : ') try: # prompt current age cur_age = int(input("Current Age (in years) : ")) except: print("Sorry you didn't inputted correct current age value!!! Try another time") return try: # prompt expected life span life_span = int(input("Expected life span (in years) : ")) except: print("Sorry you didn't inputted correct expected life span value!!! Try another time") return # calculate age in days age_days = cur_age * 365 # calculate days left to live days_left = (life_span * 365) - age_days # print final message print(first_name + " you are " + str(age_days) + " days old. You have " + str(days_left) + " days left to live.") # execution of program starts from here if __name__ == '__main__': main()
OUTPUT
(base) avianjjai@avianjjai-Vostro-3578:~/Desktop/Chegg$ python chegg101.py First name : Chegg Expert Current Age (in years) : Fifty-Five Sorry you didn't inputted correct current age value!!! Try another time (base) avianjjai@avianjjai-Vostro-3578:~/Desktop/Chegg$ python chegg101.py First name : Chegg Expert Current Age (in years) : 26 Expected life span (in years) : Fifty-Five Sorry you didn't inputted correct expected life span value!!! Try another time (base) avianjjai@avianjjai-Vostro-3578:~/Desktop/Chegg$ python chegg101.py First name : Chegg Expert Current Age (in years) : 26 Expected life span (in years) : 75 Chegg Expert you are 9490 days old. You have 17885 days left to live. (base) avianjjai@avianjjai-Vostro-3578:~/Desktop/Chegg$
Program of Test Grading:
test_score = int(input('Enter Your Test Score in the range (0-100) inclusively: '))
if test_score<0 or test_score>100:
print("Sorry, Your Input Score is not in the given Range!!! Try another time!!!")
else:
if test_score>=90 and test_score<=100:
print("Your Grade: A")
elif test_score>=80 and test_score<=89:
print("Your Grade: B")
elif test_score>=70 and test_score<=79:
print("Your Grade: C")
elif test_score>=60 and test_score<=69:
print("Your Grade: D")
else:
print("Your Grade: F")