First Assignments Sample

This article walks you through creating a simple Python program (and it just happens to be VERY similar to your first actual assignment).

Introduction

There are many things to consider when creating a Python program, no matter how simple.

  1. How should the program be named?
  2. What code format and comment standards should be used?
  3. How should the user be prompted, if necessary?
  4. What calculations are needed, if any?
  5. What should the output look like?

1. Program Name

Simple. As always, follow the course standards, which state that the program name should be

module_assignment_exercise_lastnamefirstname.py
I will use my name, and assume this is Exercise #0. In other words:
m01_assn1_ex0_marrerodavid.py

2. Code Format

Again, simple. First, the Course Standards require a program header for ALL programs. The header tells other programmers about the program, but does not display when the program runs. The program header looks something like this:

# course: sdev 140
# author: marrero, d
# date: 2021-08-15
# program: m01_assn1_ex0_marrerodavid.py:
# purpose: convert a Fahrenheit temperature to Celsius

Second, every program must display a Banner with your name and initials.

I will use the following:

# display a banner
print(' This program (by DRM) converts Fahrenheit to Celsius')

Comments should be included for each 3-4 lines of code, or where needed to explain it.

3. How should the user be prompted, if necessary?

The input statement is used to prompt the user. The user's entry can be assigned to a variable. The user's response is ALWAYS a string. The proper way to accept input is to assign it to a variable that will be considered a string. If the expected user response should be another data type, such as an INT or FLOAT, convert the string variable to the appropriate data type in a separate statement. DO NOT combine the user input and data conversion into a single line, such as

age=int(input('Please enter your age: '))
This will result in a subtraction of points!
Read the discussion on User Prompts to learn more.

This program only requires a single user prompt:

# prompt the user; response is a string
reply = input(' Please enter a Fahrenheit temperature: (xx.x) ')

To be polite and complete, I will "echo" the user's entry

# confirm the user's entry
print(f' You entered {reply}.')
print('')

I asked the user to enter a decimal number (FLOAT), when I suggested the input could be "xx.x". So I will convert the user response now into a FLOAT:

# convert to number/float
fahrenheit = float(reply)

So, the user's entry (a string) is stored in the variable "reply". It was converted into a FLOAT and stored in the variable "fahrenheit".

4. What calculations are needed?

The assignment will often give us specific calculations steps to follow. For this sample assignment, we will use the formula:
# formula to use:
# celsius = (fahrenheit - 32) / 1.8

Notice that the formula uses parenthesis to make it clear how the calculation should work. Do you remember the "order of operations" for algebra? Yeah, neither do I. So always use parenthesis to make things absolutely clear.

Here is the code:

# calculate/convert temp
celsius = (fahrenheit - 32) / 1.8

I now have a variable named "celsius" that contains the converted temperature.

5. What should the output look like?

You always have some leeway on this. Be creative - but don't skimp. More information is better. The output will be a number. If you display a result like:

33
You will have points taken off. Explain whatever you are displaying! Be descriptive. More is better.

For example:

The Celsius equivalent of Fahrenheit 92 is 33 degrees C.

Here is the code:

# display the calculation results
print(f' The celsius equivalent to {fahrenheit} is {celsius}.')
print(' Thanks for using the program')

Complete Sample Program (annotated)

Sample program with annotation
Figure 1: Sample Program with Annotation

Complete Sample Output (annotated)

Sample program output
Figure 2: Sample Program Output

Bonus Code

If you made it this far, here is the complete sample program. You may clone and modify, as needed, for your first assignment.

# d.marrero
# Convert fahrenheit to celsius
# SDEV140 m01_assn1_drm_converttemp_sample
# Program converts fahrenheit to celsius

# display a banner
print(' This program (by DRM) converts fahrenheit to celsius')

# prompt the user: 2 steps; response is a string
reply = input(' Please enter a Fahrenheit temperature: (xx.x) ')
# confirm the user's entry
print(f' You entered {reply}.')
print('')

# convert to number/float
fahrenheit = float(reply)

# formula
# celsius = (fahrenheit - 32) / 1.8

# calculate/convert temp
celsius = (fahrenheit - 32) / 1.8

# display the calculation results
print(f' The celsius equivalent to {fahrenheit} is {celsius}.') 
print(' Thanks for using the program')