Fop Prac002 1
Fop Prac002 1
Overview
Questions
How do I work with string values and variables?
How do I access the elements of strings and lists?
How can I use random numbers to simulate real world situations?
Objectives
1. Define and use more complex datatypes (strings and lists) and variations on control
structures
2. Use slicing and indexing to access elements in a list
3. Use a supplied Python package to provide random number options
4. Understand and implement simple Monte Carlo algorithms
Introduction
In this practical we will enter and modify programs to work with and explore strings and lists.
We will then use random numbers to select list items without replacement. The final two tasks
will implement two Monte Carlo algorithms: calculating Pi and tossing coins.
print('\nNicer formatting....\n')
Challenge
Modify the code to check if the pack is empty before continuing the loop.
Hint 1
Hint 2
Answer
LINE_UP = '\033[1A'
LINE_CLEAR = '\x1b[2K'
numlines = 3
for i in range(10):
if i % 2 == 0:
print(eyes[0])
elif i % 4 == 1:
print(eyes[1])
else:
print(eyes[2])
time.sleep(0.5)
for j in range(numlines):
print(LINE_UP, end=LINE_CLEAR)
If you change a few lines, note the change to the output:
PYTHON
numlines = 4
Swimming right
\
\/\
/\/
/
Challenge
How would you write code to animate the fish to swimming left and right, adding/removing
spaces to move the fish across the screen?
Making Spaces
Left or Right?
Move it!
Full Solution