CS 150 - Assignment 1

Due: Wednesday 2/22 at the beginning of class

This assignment will walk you through the basics of using Spyder and you'll write your first functions in Python.

1.   Interacting with Python [3 points]

Start up Spyder as practiced in the prelab (you should be able to do this by clicking on the spider web icon in your dock at the bottom of the screen.)

Try a few commands in the interactive shell (aka Console).
  >>>
is the prompt, meaning Python is ready for a command,
  ...
means it's a line continuation (i.e., Python is waiting for you to finish the statement) and a line without anything in front of it is generally the response from the interpreter.

When you're ready to move on, copy and paste your interactive session into a new window in the Spyder editor (select "New File" from the File menu, and erase the initial text). Don't copy more than a page. Put your name at the top. Then save this page as a text file in your assignment1 folder since you'll be submitting it. To do so, select "Save as" from the File menu, Save as type "Text files (*.txt)", and save your file as "problem1.txt" in your assignment1 folder.

2.   Your first Python program [4 points]

So far, we've only been interacting with the Python shell. This is good for some situations, but eventually, you're going to want to write longer programs that you can edit easily and persist when you quit Spyder.

What's in your file so far:

At this point, you program/file should have some statements at the top, followed by some print statements and then your definition for circle_area. Put comments above each of these sections so they are clearly delimited and then move on to the next section.

3.   A semester abroad in Europe [11 points]

For the last part of this assignment, you will write some functions of your own in the same file created for the previous section.

You're going to do a semester abroad in Europe and have decided to write a few functions that will help you out with some common questions you might find yourself asking while you're there.

  1. [2 points] Write a function called euros_to_dollars, with a single parameter, the price in euros, and the function will give you the price in dollars. Lookup the current price exchange from euros to dollars online. For example, after running your program you could type:

    >>>  euros_to_dollars(13.5)
    15.24285
    
    

    (Note: depending on the exchange rate you use, your value will be slightly different)

    Make sure that you are using the return statement and not printing the answer in your function. In particular, try running the following and make sure you get something similar:

    >>> dollars =  euros_to_dollars(13.5)
    >>> print(dollars)
    15.24285
    

  2. [2 points] Write a function called welcome that doesn't take any parameters and prints out "welcome" in some European language. For example:

    >>> welcome()
    Willkommen
    

    Remember that you can create functions with zero parameters. To call a function without any parameters, you still need to put the parentheses at the end.

  3. [2 points] Write a function called kilometers_to_miles, with a single parameter, the number of kilometers, and the function will give you the distance in miles. For example, after running your program you could type:

    >>> kilometers_to_miles(100)
    62.137
    
    

  4. [3 points] Write a function called mpg_from_metric that takes two parameters: first the number of kilometers and second the number of liters. The function returns the miles per gallon (i.e., miles divided by gallons) by converting the kilometers and liters appropriately. Remember, to have multiple parameters for functions, you separate them with commas.

    >>> mpg_from_metric(400, 30)
    31.358472666666668
    
    

  5. [2 points] Write a function of your own that takes one or more parameters and does something interesting/useful. Be creative!

  6. Extra [1 point]: The right way to calculate the mpg_from_metric function is to utilize other functions you write that do some of the work for you. To calculate the mpg, write an additional function liters_to_gallons and then use this function and your kilometers_to_miles function inside the mpg_from_metric function.

  7. Extra [1 point]: Write another function that will be useful on your European vacation. The amount of extra points given will be determined based on the usefulness of your function as well as the difficulty to implement.

4.   When you're done

At this point you should have two files in your assignment1 folder: "problem1.txt" and "assignment1.py". The first file should be in plain text ".txt" format (please don't save in any other format, e.g. ".doc", ".docx", or ".pdf"). The second file should contain all the Python code you wrote for parts 2 and 3 (be sure you followed all instructions). Also, make sure that this file is properly commented:

Submit each file using the digital submission page by the beginning of your class section on Wed 2/22.

5.   Grading

points
Prelab 2
Section 1 3
Section 2 4
Section 3 11
Comments and code style 3
Extra points 2
Total 23 + 2