Intro. to Programming : Fall 2017 -Online

Csci 1020 September 10-16, 2017 Program Assignment 4

Write a GUI which quizzes the user and provides feedback including sound.

Here are links to the sample code and necessary sound files:

s4.py
applause_y.wav
buzzer_x.wav

Sounds and Portability

Python is fairly portable, but there are certain areas which are machine-specific if one uses Python as it comes installed--which we do. Sounds can be done in many ways, and we will use WAV files, files which end in the extension .wav. You will need to uncomment exactly one method of using sounds, depending on whether you have a mac, windows, or linux machine. Do leave the options in your code so that other people (like me) can select the correct method of playing sounds depending on which machine the code is run on.

You must download the sound .wav files and have them in the same directory/folder of the python program which uses them. Here is a nice website to get free .wav sounds--for educational purposes only:

http://www.wavsource.com/sfx/sfx.htm

There are other web sites. This is one which is free and convenient.

You may, of course, record your own sounds and use them without restriction--if you can record and save .wav files. Making your own sounds from scratch is satisfying and fun if you want something special. If you end up making a product or website using sounds, you should make your own or ensure that your sounds are public domain. Free sounds above for Python programming in class are fine to use just for educational purposes.

Lists

We have not formally covered lists yet. These are called "arrays" in most other programming languages. Lists are sequences of things: integers, reals, strings, whatever. Here is a list:

cat = ["Fluffy","Spot","Rover"]

Python lists are indexed starting at 0, so cat[0] is "Fluffy", cat[1] is "Spot", and cat[2] is "Rover". Note, there are 3 items in the list cat, and the indices are 0,1, and 2.

You can add items to a list with append.

cat.append("Silver")

After appending "Silver" to the list cat, cat is now

["Fluffy","Spot","Rover","Silver"]

The list cat now has 4 items, and cat[3] is "Silver".

You program 4 code should use two lists. One list is to hold questions, and the other list holds answers 1 for True and 0 for False.

The sample code contains two questions, but the lists in the sample code have 3 items. This is done for convenience. question[0] and key[0] are unused duds, so question[1] and question[2] hold the two questions, and key[1] and key[2] hold the two answers.

You will need to make your own questions and append them to these two lists: question and key.

Why does a list (array) begin with index 0?

It does for Python, also for C and many other languages. But, some languages begin at 1 like R and Julia. Fortran used to begin at 1, and still does by default, but one can start indices in Fortran arrays any way one wishes, 0, 1, -3, whatever.

It would seem possible to start python list indices any way one wishes, but alas not, because Python uses negative indices to count back from the end of a list, so 0 must always be at the beginning.

So, why is the starting index 0? Why not 1, or why not let the user set it? The answer is probably that a computer scientist (Guido van Rossum) designed Python for his own interests, and computer scientists are almost always familiar with C which has 0-based indexing. 0-based indexing is simpler at the level of writing compilers and interpreters. I don't read minds, but I do read what computer scientists write, which is often that 0-based indexing is the most natural thing to do--meaning for them.

In any case, we have 0-based indexing with Python. Now, it is nice to number questions and key values in a quiz starting at 1 and up to the end, i.e. 1,2,3,4,5,6,7,8,9,10 for a 10-question quiz. That is natural for humans taking the quiz but also for programmers writing the code in this case. I suggest that you do this in your p4 assignment. This means ignoring question[0] and key[0] values, then adding question and key values starting at 1 and up to 10 using .append() .

Don't feel like you are writing bad code by ignoring those 0-index items! There are many cases when it is nice to use 0-indexing. In fact, in Python we often don't care about the indexing because lists of things can be used without knowing exactly where an item is in the list or even caring what a particular index is for each item.

Something Neat about Python: A look ahead..

Compared to other scripting languages, Python is easy to use with other languages like C and Fortran. We will do this in a later assignment.

To be admitted, Python is slow compared to compiled languages like Fortran and C. You won't notice this relative sluggishness which is measured in milli, micro, and nano seconds when you are using and writing GUIs and command line programs which interact with humans. You will notice it when something is repeatedly, like millions and billions of times. But, one can call C and Fortran programs very easily from Python when raw speed matters, and there are many ways to do it. We will use ctypes in our course, and we will use a C compiler which is fairly easy to install on mac and Windows machines for this class. You can also use code directly which is already on your machine.


Content is neither approved nor reviewed by FDLTCC.