1、hello and describe your result. Make note of when you see the quotation marks and when you dont.8. Type print cheese without the quotation marks. The output will look something like this:9. Traceback (most recent call last):10. File , line 1, in ?NameError: name cheese is not definedThis is a run-ti
2、me error; specifically, it is a NameError, and even more specifically, it is an error because the name cheese is not defined. If you dont know what that means yet, you will soon.11. Type This is a test. at the Python prompt and hit enter. Record what happens.Now create a python script named test1.py
3、 with the following contents (be sure to save it before you try to run it):What happens when you run this script? Now change the contents to:print and run it again.What happened this time?Whenever an expression is typed at the Python prompt, it is evaluated and the result is printed on the line belo
4、w. is an expression, which evaluates to (just like the expression 42 evaluates to 42). In a script, however, evaluations of expressions are not sent to the program output, so it is necessary to explicitly print them.2.13. Exercises1. Record what happens when you print an assignment statement:2. prin
5、t n = 7How about this? print 7 + 5Or this? print 5.2, this, 4 - 2, that, 5/2.0Can you think a general rule for what can follow the print statement? What does the print statement return?3. Take the sentence: All work and no play makes Jack a dull boy. Store each word in a separate variable, then prin
6、t out the sentence on one line using print.4. Add parenthesis to the expression 6 * 1 - 2 to change its value from 4 to -6.5. Place a comment before a line of code that previously worked, and record what happens when you rerun the program.6. The difference between input and raw_input is that input e
7、valuates the input string and raw_input does not. Try the following in the interpreter and record what happens:7. x = input()8. 3.149. type(x)10. x = raw_input()11. 3.1412. 13. 14. The knights who say ni!15. xWhat happens if you try the example above without the quotation marks?Describe and explain
8、each result.16. Start the Python interpreter and enter bruce + 4 at the prompt. This will give you an error:bruceAssign a value to bruce so that bruce + 4 evaluates to 10.17. Write a program (Python script) named madlib.py, which asks the user to enter a series of nouns, verbs, adjectives, adverbs,
9、plural nouns, past tense verbs, etc., and then generates a paragraph which is syntactically correct but semantically ridiculous (see http:/madlibs.org for examples).3.8. Exercises1. Using a text editor, create a Python script named tryme3.py . Write a function in this file called nine_lines that use
10、s three_lines to print nine blank lines. Now add a function named clear_screen that prints out twenty-five blank lines. The last line of your program should be a call to clear_screen.2. Move the last line of tryme3.py to the top of the program, so the function call to clear_screen appears before the
11、 function definition. Run the program and record what error message you get. Can you state a rule about function definitions and function calls which describes where they can appear relative to each other in a program?3. Starting with a working version of tryme3.py , move the definition of new_line
12、after the definition of three_lines. Record what happens when you run this program. Now move the definition of new_line below a call to three_lines(). Explain how this is an example of the rule you stated in the previous exercise.4. Fill in the body of the function definition for cat_n_times so that
13、 it will print the string, s, n times:5. def cat_n_times(s, n): Save this function in a script named import_test.py. Now at a unix prompt, make sure you are in the same directory where the import_test.py is located ( ls should show import_test.py). Start a Python shell and try the following: from im
14、port_test import * cat_n_times(Spam, 7)SpamSpamSpamSpamSpamSpamSpamIf all is well, your session should work the same as this one. Experiment with other calls to cat_n_times until you feel comfortable with how it works.4.13. Exercises1. Try to evaluate the following numerical expressions in your head
15、, then use the Python interpreter to check your results:1. 5 % 2 9 % 53. 15 % 124. 12 % 155. 6 % 66. 0 % 7 7 % 0What happened with the last example? Why? If you were able to correctly anticipate the computers response in all but the last one, it is time to move on. If not, take time now to make up e
16、xamples of your own. Explore the modulus operator until you are confident you understand how it works.2. if x 5. print x, is greater than6. else:7. print x, and, y, are equal8. Wrap this code in a function called compare(x, y). Call compare three times: one each where the first argument is less than
17、, greater than, and equal to the second argument.9. To better understand boolean expressions, it is helpful to construct truth tables. Two boolean expressions are logically equivalent if and only if they have the same truth table.The following Python script prints out the truth table for any boolean
18、 expression in two variables: p and q:expression = raw_input(Enter a boolean expression in two variables, p and q: )print p q %s % expressionlength = len( % expression)print length*=for p in True, False: for q in True, False: print %-7s %-7s %-7s % (p, q, eval(expression)You will learn how this scri
19、pt works in later chapters. For now, you will use it to learn about boolean expressions. Copy this program to a file named p_and_q.py, then run it from the command line and give it: p or q, when prompted for a boolean expression. You should get the following output: p q p or q=True True TrueTrue Fal
20、se TrueFalse True TrueFalse False FalseNow that we see how it works, lets wrap it in a function to make it easier to use:def truth_table(expression): length = len( print length* for p in True, False:We can import it into a Python shell and call truth_table with a string containing our boolean expres
21、sion in p and q as an argument: from p_and_q import * truth_table(p or qp q p or qUse the truth_table functions with the following boolean expressions, recording the truth table produced each time:1. not(p or q)2. p and q3. not(p and q)4. not(p) or not(q)5. not(p) and not(q)Which of these are logica
22、lly equivalent?10. Enter the following expressions into the Python shell:11. True or False12. True and False13. not(False) and True14. True or 715. False or 716. True and 017. False or 818. happy and sad19. or 20. 21. Analyze these results. What observations can you make about values of different ty
23、pes and logical operators? Can you write these observations in the form of simple rules about and and or expressions?22. if choice = a:23. function_a()24. elif choice = b25. function_b()26. elif choice = c27. function_c()28. else:29. print Invalid choice.30. Wrap this code in a function called dispa
24、tch(choice). Then define function_a, function_b, and function_c so that they print out a message saying they were called. For example:31. def function_a():32. print function_a was called.Put the four functions ( dispatch, function_a, function_b, and function_c into a script named ch04e05.py. At the bottom of this script add a call to dispatch(). Your output should be:function_b was called.Finally, modify the script so that user can enter a, b, or
copyright@ 2008-2022 冰豆网网站版权所有
经营许可证编号:鄂ICP备2022015515号-1