**Homework 1: Set-up, Script, and Submit!** **Reed CSCI 121 Fall 2022** *complete and submit Part 1 in lab* *start Part 2 in lab, submit by 9/6 at 9am* In this first homework we set-up the software necessary for completing and submitting your work throughout the semester, and we try out some Python programming. By the end of this homework you'll have run the Python interpreter, written some Python code, tested that code, and submitted it to us so that we can take a look at it. In doing so, you will interact through Gradescope with `aga`, our automated system that works to check your work and tries its best to give you feedback on it. (`aga` is named as such because (`a`)ga (`g`)rades (`a`)ssignments!) You can then start work on the programming exercises to be completed in the next week. # Part 1: Set-Up Before we dive deeply into the material of the course we first need to make sure your computer has all the software it needs for the course. This first part of the assignment mwalks you through that process. We have tried to include instructions for all the situations that might arise, but if anything isn't working, you can work with us to get things set up right. Here are the steps we take you through: * **Join Gradescope** where you'll submit the work. * **Obtain a text editor.** We use this for writing code. * **Set up a terminal console** to run Python code. * **Obtain Python 3.** This tool knows the Python programming language. * **Try the tools** to create two text files. How you take these steps will depend on your computer system. For each we will give you ways of checking whether your set up was successful. When you've completed these steps and checks, you'll submit two files using Gradescope and then move on to Part 2. ## Join the Gradescope course Your first step is to create a [Gradescope](https://www.gradescope.com) account associated with your Reed email. In doing so you can join the [CSCI 121 Fall 2022 course](https://www.gradescope.com/courses/432033) on Gradescope by using the course code we'll provide in this lab. You need to join Gradescope in order to submit the work of this assignment and all the future assignments. ## Get a text editor You will write your code in a text editor, and there are lots of options to pick from. As your next step, install one of the following editors for your system by following its link: * Windows: [**Visual Studio Code**](https://code.visualstudio.com/), [Atom](https://atom.io/), [Sublime Text](http://www.sublimetext.com/), [Notepad++](http://notepad-plus-plus.org/) * Mac: [**Visual Studio Code**](https://code.visualstudio.com/), [Atom](https://atom.io/), [Sublime Text](http://www.sublimetext.com/), [TextMate](http://macromates.com/download), vim, emacs, [AquaMacs](http://aquamacs.org/) * Linux: [Atom](https://atom.io/), [Sublime Text](http://www.sublimetext.com/), vim, emacs, [Gedit](http://wiki.gnome.org/Apps/Gedit) In lab and lecture we will encourage you to use Visual Studio **Code**, heareafter named **VS Code**, because Microsoft has made it freely available (on Windows *and* on the Mac). They have designed it to be simple and lightweight (compared to their bloated [Visual Studio](https://visualstudio.microsoft.com/) which we **do not** recommend). **VS Code** is designed in the spirit of many good and flexible program editors from the free software community. **Atom** is another good editor that many Reed CS students have adopted, and that CSCI 121 used in recent past, but the group that invented it has announced that [it will no longer be supported after December of this year](https://github.blog/2022-06-08-sunsetting-atom/). Both editors have a lot of features for writing Python code. They can be configured to do many other programming tasks too, and they both handle code in many other programming languages. Note that many shared CS computers used for upper-level classes are configured with the *console-based* editors `vim` and `emacs` (as opposed to *window-based* or *GUI* editors). The two editors are touted by many programmers and are great to learn, but they're not as beginner-friendly as the others. They are great to learn at some point as you learn CS. Maybe wait to try them a few weeks into the course. We ourselves will sometimes use `emacs` in live demonstrations because we use it in our own work. You should not use editors that you might use for writing HUM110 papers (like **Word**) for writing program code! Editors for programming create *plain text* files, which really just contain the "raw" characters of your program's text (no fonts, no decorations, etc.) so they can be processed and read by a variety of programming tools. If you tried to load, say, a **Word** document into Python, Python would just get confused. Once you have chosen a text editor, download it. Make sure it opens correctly. **Tabs and spaces** If you aren't using **VS Code** then you *might* also need to configure your editor so that it handles Python code well. In particular, you need to make sure it handles the **[tab]** key in a coding-friendly way. Your text editor needs to be set so that hitting the tab key actually inserts 4 spaces rather than a tab. This is crucial because Python is sensitive to how you indent portions of code, using that indentation to figure out what to do. It can get confused when it sees a mix of tabs and spaces in your code, so it's best to configure it to treat tab keypresses as spaces. **VS Code** already does this by default when you are editing a Python code file. This is often easy to configure, but the process for doing it is a little different for each one, and so you'll need to figure that out for the one you chose to use. If you have trouble, ask a TA for help. For example, **Atom** can be configured by following the steps in the [appendix at the bottom of this page](#appendices/appendix:configuringatomtabbing). ## Make coursework folders Make a folder called `csci121` where all your work for this course will be kept, and put it in your computer user's `Documents` folder. In the instructions below, we assume you do this. Some people find other locations more convenient, say under `Desktop` or else directly within the your computer user's home folder. If you decide to use a different location than `Documents/csci121` you will have to re-interpret our instructions and example computer interactions accordingly. Once you have this folder, create a folder inside it called `homework1` (you'll do this for each homework or project). Now download these two files into that folder: * [survey.txt](homework1/survey.txt) * [test.py](homework1/test.py) ## Start a terminal console Most computer systems provide a *terminal program* so you can work with your computer's system--- run applications and programs, move files and folders around, do low-level tweaks--- using a *command line console*, that is, by having you type commands rather than point and click. This is a typical way many programmers work, especially when they need to run their own code as it develops, and to run scripts, say to automate lots of activities like testing code and working with data files. We will type console commands into a terminal to run our Python programs. Mac and Linux already have a terminal program included with the operating system, one that is based on the Unix system. If you have a Mac or Linux, you can just run **Terminal** and that should bring up a window that looks like the console of a 1970s computer. Windows machines have a tool called **Powershell** that mimics a Unix terminal on their system. (You can find it by searching for `powershell` within the Windows Explorer search bar.) A terminal console will only display text, it will display that text in a typewriter font, and it will come up waiting for you to type in a command after printing a prompt. Here, for example, is what my terminal console looks like when I start it on my Mac: ~~~ none Last login: Fri Aug 26 06:06:50 on ttys002 jimfix@C02F48U1ML7H ~ % ~~~ The first line is a brief system message. The second line is an elaborate prompt up until the `% ` where the system waits for me to enter a command. The full prompt is in the format: ` `*My-User's-Name* `@` *My-Machine's-Name*` `*Working-Folder*` %` My user's name is `jimfix`, my computer (for some reason) is called `C02F48U1ML7H`, and the folder that **Terminal** is working in is my user's home folder on my computer, abbreviated as `~`. I can run programs that inspect files in that folder, and I can switch to other folders, and so forth. Here is a longer transcript of a **Terminal** session: ~~~ none Last login: Fri Aug 26 06:06:50 on ttys002 jimfix@C02F48U1ML7H ~ % uname -srm Darwin 19.6.0 x86_64 jimfix@C02F48U1ML7H ~ % pwd /Users/jimfix jimfix@C02F48U1ML7H ~ % cd ~/git/jimfix.github.io/csci121/lectures/week1/examples jimfix@C02F48U1ML7H examples % ls circle.py hello5.py radius_functions.py csci11squared.py hello6.py radius_import.py csci121.py hello_calc.py shoutout.py hello.py integers.py shoutout4x.py hello3.py integers_divide.py temperature.py hello4.py radius.py jimfix@C02F48U1ML7H examples % grep sqrt *.py radius_functions.py: from math import pi, sqrt radius_functions.py: return sqrt(someArea / pi) radius_import.py:from math import pi, sqrt radius_import.py:radius = sqrt(area / pi) jimfix@C02F48U1ML7H examples % ~~~ Each line starting with `jimfix` ends with a command I typed after `%`. Any subsequent lines show the system's response to that command. For example, the `pwd` command showed me the full name of the folder I'm working within. We'll demonstrate a few more of these commands in later steps. **Other options for running a terminal** It turns out that the **VS Code** editor has the ability to run a terminal console. In **VS Code** select the menu item *Terminal > New Terminal*. This will open up a terminal console just at the bottom of the editor. On a Mac system, this will run the `zsh` tool that is also run inside Mac's **Terminal** program. On a Windows system, this will run `powershell`. For Windows machines, we offer two more options in the appendices on [Git tools](#appendices/appendix:gettingawindowsterminalusinggittools) and [WSL](#appendices/appendix:gettingawindowsterminalusingwsl) at the bottom of this document. Since Mac and Linux are actually flavors of the Unix operating system, the commands we type are actually part of their system. In Windows, `powershell` is emulating those commands to run things in Windows. Because of the fact that Windows is a different system, historically Windows users have had to have complicated solutions to get a terminal console that offers Unix-like commands. The appendices give you two other, more expert ways of emulating a Unix console environment on Windows. ## Install Python The next piece of software you'll want to put on your computer is Python. If you have a Mac, Python 2 comes pre-installed, but this is not the version we will be using. You should instead download the latest version of Python 3 (currently 3.10.6) [here](https://www.python.org/downloads/). Make sure to pick the correct download for your operating system. Follow the instructions on that site to download and install Python 3. If you have a Windows machine you can install Python 3 the same way. Go to [the Python website](https://www.python.org/downloads/) and get the Windows version. (If you use Linux or [installed the Ubuntu WSL](#appendices/appendix:gettingawindowsterminalusingwsl), these already have Python 3 installed. You don't need to do anything!) ## Play with Python Bring up a terminal. Once you get a command prompt, you can enter the Python 3 command. For nearly all of you, this should be `python3`. For some Windows users this may instead be `py` or `python`. Here is a Mac terminal session that starts Python 3: ~~~ none jimfix@C02F48U1ML7H ~ % python3 Python 3.9.10 (main, Jan 15 2022, 11:48:15) [Clang 12.0.0 (clang-1200.0.32.29)] on darwin Type "help", "copyright", "credits" or "license" for more information. >>> ~~~ The key thing that you should see is the *Python* prompt `>>>`. Here you can type individual lines of Python code and watch them be evaluated immediately. If you type `4+5` and then press the `[return]` key, it should print `9` on the next line and give you a new `>>>` prompt. Try each of the Python code suggested below and make sure that you get the same responses. The lines starting with `>>>` contain the code you should type at the prompt. The other lines should appear automatically as responses. ~~~ python >>> 18 % 4 2 >>> x = 7 >>> print(x) 7 >>> from math import pi >>> pi 3.141592653589793 >>> quit() ~~~ The last line with `quit()` tells Python to exit and should return you back to the terminal so that you can enter console commands, not Python expressions. ## Run a Python program We now want to run the `test.py` file, which contains a Python program. To do this, we need to learn some more Unix commands. For now, we will learn `cd` (change directory, i.e. folder) and `ls` (list a folder's contents). When you're working in the terminal, your computer keeps track of the folder you are currently working in. Each session starts by default in your computer user's home folder. If you ever want to return directly to that home folder, you can type the `cd` command. You can display the contents of the folder you're in (both sub-folders and files) by typing the `ls` command. If the folder you're in contains the sub-folder named `inner`, you get to it by typing `cd inner`. If you want to move up one level into the parent folder containing the current folder, you do it with the `cd ..` command. For example, on my Mac, I might use the following commands below to navigate to the folder for this assignment. ~~~ none jimfix@C02F48U1ML7H examples % cd ~ jimfix@C02F48U1ML7H ~ % cd Documents jimfix@C02F48U1ML7H Documents % cd csci121 jimfix@C02F48U1ML7H csci121 % cd homework1 jimfix@C02F48U1ML7H homework1 % pwd /Users/jimfix/Documents/csci121/homework1 jimfix@C02F48U1ML7H homework1 % ls survey.txt test.py jimfix@C02F48U1ML7H homework1 % ~~~ In the terminal interaction lines above, there are prompts for a command line (e.g. `jimfix@C02F48U1ML7H homework1 %`) and I have typed the commands that follow the percent sign. In the first line, I went to my home folder, and then used three commands to go into `~/Documents/csci121/homework1` . Finally, I entered `pwd` just to verify where I was working and then `ls` to inspect the contents of that folder. Play around with the commands to get used to this kind of navigation. Should you get lost during this play, you can always type the command `pwd` (print the "working", or current, directory) to see where you are. Once you are in your `homework1` folder the command `ls` should list the two files we have put there, `test.py` and `survey.txt`. Now, enter the Python command, just like you did earlier, but include the file name `test.py` on that same line, like so. ~~~ none python3 test.py ~~~ (Recall that if this Python command doesn't work, you can try typing `py`, or perhaps `python3.10` or `python3.9` in case for some reason the system you're using isn't strictly what we'd expect from the instructions we gave above. On Windows, it might just be `python`.) Entering this full command should run that program's code, and it should print a number in the terminal. Write this number down. You'll need it in a minute. ## Problems for Part 1 • **[HW1 P1] survey** Let's begin working on the first two problems for this assignment. We'll start by editing a simple text file in your text editor. Use your chosen text editor to open the `survey.txt` file. In it, you'll see a bunch of survey questions meant to help us get to know you. Type your answers below the questions, then save the modified file. There. You've completed the first problem. If you would like, you can learn a little more about the Terminal console, and you can use it to verify the work. Go back to the Terminal and try out some of the commands below, assuming that its still working in the `homework1` folder: ~~~ none jimfix@C02F48U1ML7H homework1 % ls -ltr total 16 -rw-r--r-- 1 jimfix staff 108 Aug 27 09:12 test.py -rw-r--r-- 1 jimfix staff 735 Aug 27 09:34 survey.txt jimfix@C02F48U1ML7H homework1 % cat survey.txt 1. What is your name (as it appears officially at Reed)? James Fix, but you can call me Jim. 2. Is there anything we should know when addressing you? (Pronouns? nickname?) I use "he/him/his" pronouns. When I was young, and because I had two older brothers, I was called "little Fixer." There is a random fact. ... jimfix@C02F48U1ML7H homework1 % more survey.txt ... ~~~ The first command `ls -ltr` gives a more elaborate report of the contents of the `homework1` folder. It lists a (`l`)ong version of its files, according to their (`t`)imestamp, and in (`r`)everse order so that the most recently edited file appears at the bottom. You'll see that the number of characters in those text files are reported (the `108` and the `735` values) and the time that each was created or last edited. The other two commands `cat` and `more` are two different ways of using the terminal to inspect the contents of a file. They both show the series of lines of `survey.txt`, though the `more` command does this interactively. It waits for you to hit `[space]` if the file contents go beyond a single screen's worth of information, and to hit `q` or `[return]` (or probably most keys) when you are done looking at the file. The `cat` command just blasts all the contents out to the console without pause, even if it's a large amount of text. In both cases, you should expect to see the contents of the survey with your changes. --- • **[HW1 P2] code** Now let's write some Python code. Use the editor to create a new file where we will write the code of a Python program. In **VS Code**, for example, you would want to select the menu item *File > New Text File*. Having done that, the editor opens a new sub-window that has some helpful text written inside it. You should immediately click on the highlighted words `Select a language` on line 1 of that helpful text. Scroll through the options to find and select the Python language. Now go ahead and type the following Python code into the file, replacing any initial text that the editor provided. The file should just be this code: ~~~ python def first(a, b): c = 3 if a > b: c = 4 return a**c - b ~~~ You will want to literally type in the code, rather than cut and paste. Some characters that appear in documents look the same but are not the same as the keys you would press yourself (double quotes and hyphens are notorious examples of this trickiness). Furthermore, you should make sure that you indent each of the lines after the first using spaces-- 4 spaces for lines 2, 3, and 5; 8 spaces for line 4. (If your editor is configured correctly to treat tabs as spaces, you can tab once for lines 2, 3, and 5; hit tab twice for line 4.) Save the file with the above code as `code.py`. Most text editors will now know that your file is a file of Python code and will color it accordingly, with specific colors assigned to functions, variables, etc. You can verify that it was saved with `ls`, `cat`, or `more` in terminal if you like. You could also use your computer's file browser to see if a new file named `code.py` has been created. *Trying out your Python code* Now I know that you probably followed our instructions well and copied the code verbatim. Even so, you should want to see if the code works before you hand it in. We will explain this more carefully in future lectures and labs, but there is no harm in just trying things out now. In the terminal type the following command: ~~~ none python3 -i code.py ~~~ This is the same `python3` command you entered earlier along with some extra information. This runs the program you've written in a different way. It reads in the code in `code.py` code first, and then allows you to *interact* with Python, prompting you for more Python code. For example, here is a session that I ran with my code in the terminal: ~~~ none jimfix@C02F48U1ML7H homework1 % python3 -i code.py >>> first(100,1) 99999999 >>> quit() jimfix@C02F48U1ML7H homework1 % ~~~ Here, I enter the Python command, and then I enter a Python expression after the `>>>` prompt. Python responds on the next line with a number computed from the expression `first(100,1)` If instead you see errors rather than an integer, talk with a TA. To stop playing with the code within Python, Enter `quit()` at a `>>>` prompt to leave the Python system and go back to entering terminal commands. Understand that this is hardly testing the code, and rather just an easy sanity check compared to what we expect for later homework and projects. ## Submit your work for Part 1 We're ready for the final step, submitting the completed work. To do this, go to Gradescope (and log in) and navigate to the site's [CSCI 121 Fall 2022 course](https://www.gradescope.com/courses/432033) page. You should see several assignments there, including one called **[HW1 P1] survey** and another called **[HW1 P2] code**. When you click on each, you'll get a *Submit Programming Assignment* pop-up that will ask you to upload the file you need to submit for that problem. For "code", upload the file `code.py`, but only once you've run it yourself. For "survey" upload the file `survey.txt`, but only once you've edited and saved it with your survey responses. For each, the system should tell you that the problem was successfully submitted. The system then inspects the document you submitted and does work to give you more detailed feedback on its contents. In the case of the first problem, your submitted survey will get scanned by a script. In the case of the second problem, the code you submit will be run by a test script. For each, some results should appear shortly and a score out of 20 will get reported. Sometimes the system is slow but the score should eventually appear. Let us know if for some reason it does not. If you see an autograder score of 20 for each, you will have completed Part 1 of this assignment. Congrats! You're ready to do more interesting things in Part 2. # Part 2: Some Scripting In yesterday's lecture we covered some key elements of the Python language. We showed examples of some simple Python *interactive scripts* that can be run in the terminal. Python allows your program to read what's entered into the terminal (using `input`) and to report computed values (using `print`). In this Part 2 we write a couple scripts, test them to see that they work, and then submit them to Gradescope. Below, for example, is a short three-line script that performs temperature unit conversion, getting a temperature in degrees celsius, then reporting the equivalent in degrees fahrenheit: ~~~ python c = float(input("Temperature in degrees celsius? ")) f = (c * 9.0 / 5.0) + 32.0 print(f) ~~~ Running the code, we see that `input` produces a prompt, then waits for the user to enter a value. Python reads their entry as a string of characters, and so we use `float` to convert those characters to a floating-point numeric value. The second line, a variable assignment to `f` using the value `c`, computes the floating point value according to that calculation. The value of variable `f` gets printed because of the last line of the script. Here's what happens in Terminal when we run that code using the `python3` command and, say, enter `100.0`: ~~~ none jimfix@C02MX1KLFH04:homework1 jimfix$ python3 temperature.py Temperature in degrees celsius? 100.0 212.0 ~~~ It's possible to make the output of `print` more readable by including other text in that line of output. To do this, we can explictly convert the temperature value to a string of characters using the `str` conversion function and then append strings around that formatted information. We append strings using the `+` operator. For example, if we change the last line of the code above to ~~~ python c = float(input("Temperature in degrees celsius? ")) f = (c * 9.0 / 5.0) + 32.0 print("That is " + str(f) + " in degrees fahrenheit.") ~~~ it leads to this interaction ~~~ none jimfix@C02MX1KLFH04:homework1 jimfix$ python3 temperature.py Temperature in degrees celsius? 100.0 That is 212.0 in degrees fahrenheit. ~~~ Note the spaces that we used to separate the number from the other text of the sentence. Each of the exercises below asks you to write a Python script similar to the one above. Each program you write takes an input, or a series of inputs. It then performs calculations on that input data and produces a formatted output of those calculations' results. We first ask you to work with strings alone. You then work with integer and floating point values. For these you'll want to convert input strings into numbers using the `int` and `float` functions. ## Submission and Autograding You should run your scripts several times, and with different inputs, to make sure that your code is working. Once you have reasonable confidence that it is working, submit it at the appropriate assignment link on [the Gradescope site for this course](https://www.gradescope.com/courses/432033). When you upload the file for that problem on Gradescope we'll immediately run a series of tests on that code to see if it is correct. Within a minute or so, you'll get feedback from our `aga` Gradescope autograder" as a series of messages. If all these tests pass, then these messages will be green. Any failed tests will give a red message, and usually with some extra information underneath that can help you figure out what we found wrong with the code. Our ability to give feedback in this way is limited, so you should seek help from your instructor or a tutor to help you explain what went wrong, and to help you think about fixing the problem. On some rare occasions some of you may face something that's wrong with our set-up so it's helpful to reach out to us if you are confused by the autograder's report. There will be those moments when there's something we didn't anticipate in your submission, hopefully rarely. Do not use Gradescope to test your code. Instead run the code yourself using Python's interpreter and only submit once you're convinced that the code is correct. If you fail some of our tests that should give you some pause. Take a moment to carefully look through your code and fix it, and then run some of your own tests before submitting it again. Don't just make a quick change, cross your fingers, and submit it again. **Scoring** Each problem is worth 30 points. 20 of those points are assigned by the aga auto-grader, and the remaining 10 will be assigned later by one of us. The auto-graded score is determined by some thoughtful test inputs that we've devised to check different scenarios. Our use of automated testing is meant to mimic real-world software coding, where a programmer's code has to work in practice, sometimes facing unanticipated but perfectly reasonable scenarios. The hand-graded score reflects our sense of your coding. Was it clear and succinct? Or did it do something unusual just to get the tests to pass? When we get to more complicated coding, these points will help us give feedback on code "style." Making complicated code human-readable and human-understandable is often as important as whether the code works, especially for larger coding projects. We split the auto-graded score into two components. We give points for passing some of the tests. This helps us to at least see the progress you've made in tackling the problem. This will be especially valuable for later assignments when you've figured out most of the problem but maybe haven't considered some tricky “edge cases” yet. The auto-grader also awards a “prize” component with its scoring. To get this prize, your code has to pass all the tests by the assignment deadline, and without too many attempts. If you submit answers for a single problem several times, with only incremental fixes, you get fewer prize points than if you test your code on your own, anticipate all the different cases, and get it working, say, on your first or second submission. Both the test score and the hand-graded score allow us to reward you for figuring out most, if not all, of the problem's puzzle. And the hand-grading allows us to give feedback on the quality of your code. The prize score is an additional incentive that encourages you to work on assignments in a timely fashion and to carefully test your code rather than just blindly submit it with little thought. In summary here is how we score your work: * **test score:** *(automated)* partial or full credit for passing tests * **style score:** *(by hand)* partial or full credit for clear, succinct, readable code; and for good attempts that maybe miss some cases * **prize score:** *(automated)* credit for passing all tests on time, discounted if you submit too often because you didn't anticipate some test cases ## Problems for Part 2 • **[HW1 P3] hal** In the science fiction movie *2001: A Space Odyssey*, the artificial intelligence of the `HAL 9000` computer, when commanded to do something, tells the main astronaut character: ~~~ none I'm sorry, Dave. I'm afraid I can't do that. ~~~ Write a Python script `hal.py` that replicates this interaction. It should prompt for a name to be entered, and then take a string as input. It should then prompt for an action it should take, and then take a second string as input. Having obtained those two input strings, your program should then output a contrarian's reply using that name, and then report that the action was not taken. Here, for example, is a transcript from a terminal execution of the command `python3 hal.py`: ~~~ none What is your name? Dave Ok. Now enter an action: Open the pod bay doors. I'm sorry, Dave. I'm afraid I can't do that. (The action "Open the pod bay doors." was not performed.) ~~~ In that example, the first two lines are each a combination of a printed prompt (each ending with a space character) followed by the characters entered by the person running the program. Here, they entered `Dave` and hit the `[return]` key and then `Open the pod bay doors.` followed by the `[return]` key. After these two entries, a blank line and then the next two lines are printed by the script. Here is another example of an expected interaction with your program when run: ~~~ none What is your name? stanley kubrick Ok. Now enter an action: cut!!! I'm sorry, stanley kubrick. I'm afraid I can't do that. (The action "cut!!!" was not performed.) ~~~ Your program's interactions must exactly mimic the examples above. There should be a space between each prompt and the program user's input. There should be one space before the name and a period immediately following it. There should be a double quote character just before and just after the action. Note that, even though the document you are reading (this one here) might make it appear so, there should be no spaces at the start of each line. The lines are each left justified. Note that you can output a blank line with the python statement ~~~ print() ~~~ that is, by giving `print` no argument within the parentheses. Do your best to test your code and check the output, comparing it with our expected output. Be wary of your spacing before you submit. The interaction's transcript is in a fixed-width (i.e. monospaced or "typewriter") font, so each character, including spaces, appears in its own column. This means that the output characters can be seen to line up within a grid as depicted below: ![*Figure.* Gridding of terminal output.](homework1/hal-grid.gif) Now, with all that being said, get that AI working! (Or, I suppose, get it *not working*--- HAL is *stubborn* and unwilling to do anything.) **Tips** A big issue here is that there are two ways to describe a string of characters in Python. For example printing a `Hello.` line can be expressed in a program as ~~~ print("Hello.") ~~~ using double quotes around the string of characters and also instead using single quotes with ~~~ print('Hello.') ~~~ But this means that the single quote character `'` and the double quote character `"` have special meaning in this code. So, for example, you have to do extra work if you literally want a quote within a string. For example, `Don't.` could be expressed in two ways: ~~~ print("Don't.") print('Don\'t.') ~~~ The second form uses the "escape" backslash character `\ ` to alert Python that a special character is coming next. So we read `\'` as "escape to quote" There is also `\\ ` for "escape to backslash", `\t` for "escape to tab", and `\n` for embedding an escaped request to start a new line within the middle string. --- • **[HW1 P4] temperature** I've written a simple interactive script, downloadable [**as this linked file**](homework1/temperature.py) called `temperature.py` that requests an input, a numeric value that will be treated as a temperature in degrees celsius. The program, after that amount is entered, reports the equivalent temperature in degrees fahrenheit. Here is the Python code for that program: ~~~ python c = float(input("Temperature in degrees celsius? ")) f = (c * 9.0 / 5.0) + 32.0 print("That is "+str(f)+" in degrees fahrenheit.") ~~~ And here is an example of its execution in terminal: ~~~ none C02MX1KLFH04:homework1 jimfix$ python3 temperature.py Temperature in degrees celsius? 40.5 That is 104.9 in degrees fahrenheit. ~~~ The program takes a string as input and, assuming that its user types in a valid number, converts that string to a floating point value (with the `float(•)` conversion we described in lecture). It uses the variable `c` to store that value. It then assigns the variable `f` to the appropriate number of degrees, according to the celsius-to-fahrenheit formula. It then outputs that amount, but has to do so by converting the floating point value held in `f` to a string (with the `str(•)` conversion we also described in lecture). Modify the code so that it instead asks for a temperature in degrees fahrenheit and reports the equivalent in degrees celsius. Your program should lead to interactions like this one: ~~~ none C02MX1KLFH04:homework1 jimfix$ python3 temperature.py Temperature in degrees fahrenheit? 104.9 That is 40.5 in degrees celsius. ~~~ Just like in the first exercise, you should get the spacing exactly right before you submit your code. --- • **[HW1 P5] distance** We're writing a new app that allows people in Portland to find where they are relative to Reed's campus, one that presents a map to the app's user. The center of Eliot Circle is the origin of the map. When a person is walking around, the app gives a two-dimensional coordinate of their location, $(x,y)$, giving them a position within a grid laid over the map. The $x$ coordinate indicates how many miles east of Eliot Circle they are. The $y$ cordinate indicates how many miles north of Eliot Circle they are. At any moment, the app tells them their distance from Eliot Circle, and that is computed (using the Pythagorean Theorem) to be $$\sqrt{x^2 + y^2}$$ that is, the square root of the sum of the square of the distance east with the square of the distance north. We want to enhance the app so that it allows a student to figure out their distance from a classroom. If they are located at $(x_1,y_1)$ and the classroom is located at $(x_2,y_2)$, then their distance from that classroom is $$\sqrt{(x_2 - x_1)^2 + (y_2 - y_1)^2}$$ Write a Python script `distance.py` that allows you to experiment with this distance calculation. It should take the $x$ and $y$ coordinates of one location, and then the $x$ and $y$ coordinates of a second location. It should then report the distance using the above calculation. ~~~ none Location x-coordinate? 3.2 Location y-coordinate? 1.5 Classroom x-coordinate? 6.2 Classroom y-coordinate? -2.5 Distance: 5.0 ~~~ Note that you can compute the square root by using the exponentiation operator `**` and with an exponent of `0.5`. It's also possible to `import` Python's `math` package which defines a function `sqrt`. Either is fine, one requires that you practice using `import`. To import, put this line at the very top of the code, before the script that does the work: ~~~ python from math import sqrt ~~~ and then any code below that line will know about the function `sqrt`. --- • **[HW1 P6] marquee** Write a Python program `marquee.py` that attempts to mimic movie theater marquee signs, putting a word "*in lights*". To get an idea of what we want, below is an execution of the program: ~~~ none Word? hello ********* * hello * ********* ~~~ The first line is the input prompt, along with an entry of the word `hello`. The second output line is just a blank line. The next three lines show the word decorated with surrounding asterisks. Here is a second interaction: ~~~ none Word? The Big Lebowski ******************** * The Big Lebowski * ******************** ~~~ To do this you need to use some string operations. We've been using string concatenation, designated with the `+` plus sign above. Recall also that the `*` asterisk can be used for string repetition. For example, the expression `'hi'*5` means `'hihihihihi'`. Here are some other examples of string repetition in Python: ~~~ python >>> 'hi'*2 'hihi' >>> 'hi'*0 '' >>> 'hello ' + ('hey ' * 3) + 'bye' 'hello hey hey hey bye' ~~~ The other string operation you need is a function that computes the length of a string, `len`. It gives the number of characters in the string's sequence. Here are some example uses of `len`: ~~~ >>> len('hello') 5 >>> len('Hello, I must be going.') 23 ~~~ Note that it counts each space, each letter, and each punctuation mark laid out between the quotes. String concatenation, repetition, and length should be enough tools needed to make `marquee.py`. # Appendices ## Appendix: configuring Atom tabbing You can configure **Atom** to use spaces by going to the menu bar at the top of the screen and finding the menu selection *Atom > Preferences...*. This will bring up a *Settings* pane with an option list *Core*, *Editor*, etc. Select *Editor* and scroll through the various configurable options scanning for a check-box named *Soft Tabs*. Select this option with a check mark so that "soft tabs" are enabled. "Soft" means that when you hit the `[tab]` key a number of spaces will be entered instead. ("Hard" would mean that an actual tab is inserted.) You should also change the *Tab Length* setting (just a little below the soft tabs setting). Set its field to `4`. **Atom** can be clever in reading source code that was written using different settings. It is willing to work with both hard tabs and soft tabs, mimicking the file it is reading. If you set the option *Tab Type* to `soft`, then it will just always insert spaces when you hit `[tab]` regardless of the other sections of the code. This is probably the option you should prefer. ## Appendix: getting a Windows terminal using Git tools A different option for getting a terminal on Windows that has worked for CSCI 121 students in the past is to download and install a program provided by the GitHub service, as part of its Windows tools, called [GitBash](https://gitforwindows.org/). If you can get this installed, it works the same as the terminal programs on the Mac and on Linux. ## Appendix: getting a Windows terminal using WSL A fairly advanced option for getting a terminal on Windows is one used by students taking our CSCI 221 course. You can install a **Windows System for Linux** or **WSL** on Windows. A WSL is a bit more feature-rich than Powershell and GitBash (described just above). It essentially has you put a mini Unix computer system (sometimes called a "virtual machine") onto your computer. This will be especially useful after this course, as you'll probably see need to use other Unix tools beyond those for Python programming, and the skills you've learned in this class will probably motivate you to do so. To install a WSL open up the **Microsoft Store** and search for **Linux**. This will bring up several Linux distributions as applications that you can download. Select the **Ubuntu** one and install it. Alternatively, you can probably just search directly for "Ubuntu" in the store. Once you've downloaded that software application, go ahead and run it. Running the Ubuntu WSL brings up a Unix console window, one that will (because it is your first time running it) ask you to name your WSL user and provide its password. I recommend using your Reed login name and password here, but you can choose whatever you like. This username/password combo will be what you type in every time you run this Ubuntu WSL application. Because this acts like a computer within your computer, it acts like it has its own structure of files and folders. But you can access your normal Windows system files and folders (the stuff that's normally on your `C:` drive) using a special "folder path" here within WSL. For example if you type the command ~~~ none cd /mnt/c/Users/WINDOWS-NAME/csci121 ~~~ where `WINDOWS-NAME` is your Windows user name, this will take you into the folder we had you make with the "**Make coursework folders**" step up above. If you don't know your Windows user name, typing the command ~~~ none ls /mnt/c/Users ~~~ should give you a list of possible user names on the system, and it should hopefully be obvious which one is yours. It's also possible to create a shortcut to your coursework folder within this WSL subsystem. If you enter the commands ~~~ none cd ~ ln -s /mnt/c/Users/WINDOWS-NAME/csci121 csci121 ~~~ then this will create a "symbolic link" called `csci121` in your WSL user's home, and that is a system item that refers to the actual `csci121` folder in your Window's user's home. This all sounds complicated, and it's not necessary for this course. Nevertheless, some of you might enjoy giving it a try.