Exercises
Unless specified otherwise, use IPython sessions for each exercise.
-
3.1 (Validating User Input) Modify the script of Fig3.3. to validate its inputs. For any input, if the value entered is other than 1 or 2, keep looping until the user enters a correct value. Use one counter to keep track of the number of passes, then calculate the number of failures after all the user’s inputs have been received.
-
3.2 (What’s Wrong with This Code?) What is wrong with the following code?
a = b = 7
print('a =', a, '\nb =', b)
First, answer the question, then check your work in an IPython session.
-
3.3 (What Does This Code Do?) What does the following program print?
for row in range(10):
for column in range(10):
print('<' if row % 2 == 1 else ...
Get Intro to Python for Computer Science and Data Science: Learning to Program with AI, Big Data and The Cloud now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.