A maior rede de estudos do Brasil

433 pág.
Apostila PyOpenGL

Pré-visualização | Página 7 de 50

the end of this section. 
 If the statement in the if line is TRUE, then any indented15 lines immediately 
below it are executed (not shot… but implemented by the Python interpreter). The 
single indented line is: 
 print n, x 
 This line prints both the Super-3 number n and it’s value x after cubing and 
multiplying by 3 (so you can see for yourself the “333” in the number). Remember, this 
line is only “executed” in the event that that if statement above it is TRUE. 
14 Think about this! It is easy to become confused with conditional if statements. 
15 Indented lines are crucial in Python. Most other languages ignore indents and they are used to 
make code easier to read. In Python, indents are part of the language and MUST be used 
appropriately. Note the indented lines after the for statement. All those lines are in the for 
loop. Likewise, the indented lines immediately after the if statement are part of the if 
 Well, that’s all for the first program. In the next chapter we’ll being working with 
graphics. First, though, we are going to try some exercises. These problems will require 
you to modify the program we’ve just discussed. If you want to save your original code, 
you’ll need to name your newly modified program something different. Suggestions for 
names might be ch3ex1.py for Chapter 3 Exercise 1… but you are free to name the 
modified programs anything you or your instructor wish.16 
Section 3.2 Conclusion 
 Computers are great at handling input, doing repetitive tasks, producing 
calculations, making decisions, and displaying output. The simple Super-3 program we 
used as an example in this chapter demonstrated the power of a computer in performing 
all of these operations in just a few lines of code. How long do you think it would take 
you to find all the Super-3 numbers less than 10000 by hand? 
1) Super-d numbers are a more general case of Super-3 numbers.17 With Super-d 
numbers, you replace d with the number of your choice. For example, Super-4 
numbers would be those numbers when raised to the 4th power and multiplied by 4, 
contain “4444” somewhere in the calculated value. How would you modify the 
Super-3 program to find Super-4 numbers? What is the smallest Super-4 number? 
2) Are there any Super-5 numbers less than 10000? What is the smallest Super-5 
number? Remember to search for “55555” in the “if” statement! 
3) Can you modify the program to search for other patterns? How about the pattern 
4) What happens if you change the formula in line 7 to something other than the Super-
d format? You won’t be searching for Super-d numbers, but perhaps you will find 
something interesting? Feel free to explore a bit! Search for strange patterns! For 
example, search for “314159”. 
5) This is a thought question. How many Super-3 numbers are there? Could you use a 
computer to find them all? 
6) Rewrite the Super-3 program to check for a match using the ">" operator or the ">=" 
7) Add some statements to the program to print the values returned by string.find 
when the function actually finds a match. 
The following exercise is a bit more difficult. 
16 But please don’t forget the “.py” suffix! I know, I already told you this but it’s really important. 
17 http://mathworld.wolfram.com/Super-dNumber.html 
8) Python has other methods of looping such as the statement while. Research other 
looping methods for Python and see if you can rewrite the program using a different 
looping structure. Remember that indentation is important! Also, you will probably 
discover that you need to be able to find a way to count or increment the value of the 
variable you are using to construct Super-3 numbers. How can you do this? Do you 
need to tell Python that the variable is going to store a number rather than a string? 
How would you do this? 
Chapter 4 Your First OpenGL Program 
Section 4.1 The Interactive Python Interpreter 
 Before we begin with OpenGL, I think it’s appropriate to show you a feature of 
Python that we haven’t yet discussed. Start DrPython (if it isn’t already open) and press 
the “green Python” button to the right of the program start icon. Your screen should look 
something like Figure 4.1. 
 Figure 4.1 
 Note the “>>>” in the console area. This prompt tells you that you are in the 
interactive Python mode. You can type Python commands at this prompt and they will 
be immediately executed. For example, type 3+7 and press enter. It seems Python 
knows a little arithmetic! Now type 10/5 and press enter. Of course, Python tells you 
the answer is 2. Now type 10/4 and press enter. What gives? Python still answers 
with 2! The reason for this behavior is that Python will perform arithmetic according to 
the numbers we give it. If we supply only integers in our problem, the results of Python 
arithmetic will be an integer. Try 10/4.0 or simply 10/4. and see if you get the answer 
you expect. As long as one of the numbers we use in the calculation is a floating point 
or decimal value, then Python will respond with floating point calculations.1 
 Python arithmetic may seem quirky, but once you have some programming 
experience, it isn’t difficult. Just remember the integer vs. floating point format to avoid 
program calculation errors. You might try multiplying, adding, subtracting, and raising 
some numbers to a power to see if you get the results you expect. 
 Before we close the interactive session, type print ‘hello’ and press enter. 
No surprise, right? Now type name = ‘Dubbya’ and press enter. Nothing happened! 
Well, actually something DID happen… what was it? Type print name, press enter 
and see if you were correct. Yes, the string ‘Dubbya’ was stored in the variable name 
and we can view the variable contents by using print. 
The interactive mode can be useful to perform quick calculations or to check 
simple program structures. Before we exit, notice in the message in the lower right 
corner of the DrPython window. It should say “Running Python Interpreter”. This tells us 
that we are in the interactive mode. Exit the interactive mode by clicking the red 
(square) stop button and then clicking on the green monitor button to the left of the 
program start icon.2 
Section 4.2 Introducing Python OpenGL 
 We are ready for our first Python OpenGL program. Start DrPython3 and type in 
the following lines in the upper programming area, starting with line 1: 
# First Python OpenGL Program 
# ogl1.py 
What do these lines do? Remember, they are remark or comment statements, 
so they really don’t DO anything other than provide notes or reminders for you and 
anyone else who might read your code. I can't emphasize enough the importance of 
using comment statements in your code, particularly as the complexity and length of 
your programs increase. 
Now, save the work in your directory as you have done previously (using “File" 
and "Save As”) and giving it a name such as ogl1.py. It’s a great idea to save your work 
every few lines or so.4 Once you have named and saved the program the first time, you 
can simply push the “diskette” button next to the printer icon to save any new code. You 
1 You should look up floating point, integer, double precision, and single precision numbers. 
Better yet, visit www.Python.org and search in the online documentation for Python data types. 
2 The green monitor button is useful for closing the console and displaying full screen editing 
mode. You must click the white stop button to stop the Python interpreter. 
3 You

Crie agora seu perfil grátis para visualizar sem restrições.