convert to int
use a loop and the int
function to find all the numbers embedded in the quote given below.
- hint: use
try/except
so that things which are not numbers wont stop your loop
expected outout:
[3, 3, 3, 4, 2, 3, 5]
### useful: use the `int` functions to grab all the numbers from this string
holy_hand_grenade_of_antioch = """\
First shalt thou take out the Holy Pin,
then shalt thou count to 3 , no more, no less.
3 shall be the number thou shalt count, and the number of the counting shall be 3 .
4 shalt thou not count, neither count thou 2 , excepting that thou then proceed to 3 .
5 is right out."""
finally
we’ve provided an important function called answer_to_universe_and_everything()
- open a file called
'answer.txt'
for writing - use a loop to call
answer_to_universe_and_everything()
a 100 times, and write the results to the file - use a
try/except
block to make sure that if answer_to_universe_and_everything() fails, we continue to call the function - if the function fails, write the fail message to the file
- use a
try/finally
block to make sure we close the file - make sure the file indeed has exactly 100 lines
### useful: use this function
def answer_to_universe_and_everything():
import random
x = random.randint(0, 10)
if x<3:
return 'need more time'
elif x<6:
return 'blue ... no, red!'
elif x<9:
raise Exception('too hard')
else:
return 42
### useful: show that the file has 100 lines
assert len(open('answer.txt').readlines()) == 100