Skip to content

Commit

Permalink
overs for tonight
Browse files Browse the repository at this point in the history
  • Loading branch information
BenHollamby committed Apr 1, 2021
1 parent 6607c0a commit f5642eb
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 27 deletions.
19 changes: 19 additions & 0 deletions Chapter 10 Files and Exceptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -177,3 +177,22 @@ def count_words_in_book(filename):
for file in filename:
count_words_in_book(file)

#Addition: One common problem when prompting for numerical input occurs when
#people provide text instead of numbers. When you try to convert the input to an int, you’ll get
#a ValueError. Write a program that prompts for two numbers. Add them together and print
#the result. Catch the ValueError if either input value is not a number, and print a friendly error
#message. Test your program by entering two numbers and then by entering some text instead
#of a number.
while True:
number_one = input("What is the first number you would like to add? Enter to quit. ") #should really be testing for int here
if number_one == '':
break
number_two = input("What is the second number you would like to add? Enter to quit. ")
if number_two == '':
break
try:
value = int(number_one) + int(number_two)
except ValueError:
print("Please enter a number ")
else:
print(value)
36 changes: 9 additions & 27 deletions exercises.py
Original file line number Diff line number Diff line change
@@ -1,36 +1,18 @@
#Addition: One common problem when prompting for numerical input occurs when
#people provide text instead of numbers. When you try to convert the input to an int, you’ll get
#a ValueError. Write a program that prompts for two numbers. Add them together and print
#the result. Catch the ValueError if either input value is not a number, and print a friendly error
#message. Test your program by entering two numbers and then by entering some text instead
#of a number.
while True:
number_one = input("What is the first number you would like to add? Enter to quit. ")
if number_one == '':
break
number_two = input("What is the second number you would like to add? Enter to quit. ")
if number_two == '':
break
try:
value = int(number_one) + int(number_two)
except ValueError:
print("Please enter a number ")
else:
print(value)



#Addition Calculator: Wrap your code from Exercise 10-6 in a while loop so the user
#can continue entering numbers even if they make a mistake and enter text instead of a number.



#Cats and Dogs: Make two files, cats.txt and dogs.txt. Store at least three names of cats in
#the first file and three names of dogs in the second file. Write a program that tries to read these
#files and print the contents of the file to the screen. Wrap your code in a try-except block to
#catch the FileNotFound error, and print a friendly message if a file is missing. Move one of the
#files to a different location on your system, and make sure the code in the except block
#executes properly.
catfile = "catfile.txt"
dogfile = "dogfile.txt"

while open(catfile, 'w') as file_object:
cat_content = file_object.write("Pork Chop\n Tiger\n La'FaShe")

while open(dogfile, 'w') as f:
dog_content = f.write("Barney\nJack\nDuke")




Expand Down

0 comments on commit f5642eb

Please sign in to comment.