Focus on the Conditionals. We will cover recursion later, but do read through the entire chapter and do the interactive examples using IDLE and a python interactive session.
Program p4 makes heavy use of conditionals, what most programmers call "if tests".
Program 4 also uses lists, a topic covered just enough for practical use within the program 4 assignment description. Some might know "lists" as arrays. We will spent special time just on lists later.
Here is a chunk of s4.py code where the sound methods are chosen. Below the
code is for a linux machine. If you have a mac, comment out that linux line (a
# at the beginning) and uncomment the line for mac. Similarly, have the correct sound
playing method uncommented in the function play_sound() for your machine.
# uncomment exactly one of these to suit your machine #import winsound # windows #import subprocess # apple mac import os # linux desktop from Tkinter import * # ------------------------------------------------------------- W = Tk() # create the window where everything is placed def play_sound(x): # x is the sound file name #subprocess.call(["afplay",x]) # mac, uncomment for #winsound.PlaySound(x,winsound.SND_FILENAME) # windows, uncomment for os.system("aplay "+x) # linux, uncomment for
You will need to pick some sounds, find them or make them, and this is discussed in the p4 assignment web page.
Here is a WAV sound which may or may not play on your browser:
Select different ones for your p4 assignment than the ones in the s4.py sample. These are tested to work on mac, windows, and linux machines on campus in stock Python, but differently as in the example code above.
The IDLE editor is for Python, so it will automatically indent your code properly when needed. You have to un-indent a line to bring indentation back. For some programming lingo, a "block" of code is indented at the same level.
Below, the text in green is what is executed when function answer() is called, and note that all the code following is within the function answer() until the indentation is brought back for the code in red.
# this is called when an answer is clicked on def answer(): # definition answer = V_answer.get() global qnum,right,wrong if(answer == key[qnum]): V_response.set("Correct") right = right + 1 play_sound('applause_y.wav') else: V_response.set("Wrong") wrong = wrong + 1 play_sound("buzzer_x.wav") V_response2.set("you got "+str(right)+" right and "+str(wrong)+" wrong.") # the answers B_a1 = Radiobutton(W,text="True",variable=V_answer,value=1,command=answer) B_a1.grid(row=2,column=0,columnspan=3,sticky='W') B_a2 = Radiobutton(W,text="False",variable=V_answer,value=0,command=answer)
Below, and within the answer() function block, the 3 lines after if(answer == key[qnum]): are excuted if answer == key[qnum] , else the 3 lines after else: are executed because the answer is not the same as key[qnum]. Note that the last line in the function (in red) is indented back, so it is not within any if or else block, and it is always executed after whatever happens in the if or else blocks.
def answer(): # definition answer = V_answer.get() global qnum,right,wrong if(answer == key[qnum]): V_response.set("Correct") right = right + 1 play_sound('applause_y.wav') else: V_response.set("Wrong") wrong = wrong + 1 play_sound("buzzer_x.wav") V_response2.set("you got "+str(right)+" right and "+str(wrong)+" wrong.")
Python is a somewhat unique language about using indentation (white space) to control the logic. Not many popular languages use indentation. It is something to be careful about using Python! As a rule, NEVER mix tabs with spaces. Better, just never use tabs. Pick some sort of rule on indentation and stick with it. A popular rule is using 3 spaces for every indentation. (Some like 4 spaces, whatever, but always do it the same in all codes, and be wary about copy and pasting from other codes.) This will make it easier when cutting and pasting code. I'll try to always use 3 spaces for indents in sample codes, as idle does by default, so life will be easier if you use 3 spaces too.
Other topics to the side...
Your textbook talks about classes, objects, methods, polymorphism, inheritance and such starting at chapter 12. There are those who promote OOP from day 1 and OOP-only for learning programming. I sharply disagree. OOP is important and Python is a good language for learning it--but later.
In fact, you are already using class objects and their methods when you use Tkinter, and this is a very good thing. Almost all GUI-building code exploits OOP. It makes writing GUIs much simpler.
You may come across such things in your reading and, especially, web browsing about Python. Some people like OOP, so promote it. Some like functional programming, hence promote that. Don't worry about it. Python is interesting in that a mix of imperative, OOP, and functional programming can be done as it makes sense, or as one feels like it. Other languages force one thing, like OOP in Java, or functional in Scheme.