Patrice Koehl
Department of Computer Science
Genome Center
Room 4319, Genome Center, GBSF
451 East Health Sciences Drive
University of California
Davis, CA 95616
Phone: (530) 754 5121
koehl@cs.ucdavis.edu




Introduction to Computers: Fall 2013

Lab7: Programming with Python (I)

This is your first programming assignment!
As an introduction to the world of programming, it is designed to be simple, but you will see how fast we will progress in the coming weeks.

Obviously, you will program using Python...

Step 1: Getting ready

Please use IDLE as your interface to using Python. Create a file in which you will save your program. To do this using IDLE:

  • Step 1: In the menu File, choose "New Window". IDLE will then create a new window named "Untitled"
  • Step 2: Again in the menu File, choose "Save as..." and give a name to the file: make sure you add the extension .py;
    for example, you can choose the name: lab6.py.
    With the extension .py, IDLE will recognize the file as containing a Python program, and will color it properly.

Now you are ready to write your program!

Step 2: Generate proper header

It is always worth documenting your program as much as you can!
For example, it is important that the file contains the name of the programmer, as well as the date the file was created.

In your file, add the following lines at the top:

#
#   Author:          Your name
#   Student ID:    Your student ID
#   Date:             Today's date
#

where obviously, "Your name", "Your student ID" and "Today's date" refer to you and the date you are working on this program!

Step 3: Programming

You will now solve three small programming exercises; put your solutions to all problems in the file you are working on, one below the other.

Two important commands you will need:

  • Input information in a program using standard input: use the command
    raw_input("String")
    where "String" will be printed on the screen (corresponds to the question asked);
    raw_input returns a string; if you expect a real number, just add float.
    Example: x = float(raw_input("Enter the value for x : "))
  • Output information on the screen: just use print;
    Example: print "The value of x is :",x,"\n"

Remember to test your program! To do this using IDLE:

From the "Run" menu, choose "Run module". This should start the program, taking you back to the main IDLE window.

Problem 1: dealing with numbers

Write a Python program that reads from standard input the amount of a restaurant bill and outputs two options for tipping, one based on 15% of the bill, the other based on 20% of the bill.

Problem 2: dealing with strings

Write a Python program that:

  • Reads a sentence from standard input
  • Writes this sentence on standard output all in lower case
  • Writes this sentence on standard output with all vowels in upper case and all consonants in lower case
  • Writes the sentence backwards

Problem 3: Counting words and characters

Write a Python program that:

  • Reads a sentence from standard input
  • Counts the number of words and the number of characters, not including space

For this problem, you will probably need the function len(): len(string) gives the length of string.

Step 4: File submission

Once you are sure your program is working, submit your file on Smartsite.

Please submit your report as a single document (you can name this document as you want, but do keep the .py extension).

It is very important to turn in your assignment. If you do not turn in, you will not get your credit.
USE SMARTSITE to save your assignment. If you are still not sure how to do it, ask a TA for help.

Grading:


The maximum score you can get is 60. The breakdown is:
Overall presentation Correct heading (with name, student ID and date) 10 points
Exercise 1 Correct execution 10 points
Exercise 2 Reading the sentence
Write the sentence in lower case
Write the sentence with vowels in uppercase
Write the sentence in reverse order
5 points
5 points
5 points
5 points
Exercise 3 Reading the sentence
Count number of words and characters
5 points
15 points

You will get credit for a question only if it is properly executed...

Do not forget to logout from the lab computers!





  Page last modified 17 December 2015 http://www.cs.ucdavis.edu/~koehl/