第十章练习10.3
x = input("请输入第一个数字:")
y = input("请输入第二个数字:")
try:
int(x)+ int(y)
except ValueError:
print("请输入数字!")
else:
print(int(x) + int(y))
print("Give me two numbers, and I'll add them.")
print("Enter 'q' to quit.")
while True:
first_number = input("请输入第一个数:")
if first_number == 'q':
break
second_number = input("请输入第二个数:")
if second_number == 'q':
break
try:
answer = int(first_number) + int(second_number)
except ValueError:
print("只能输入数字!")
else:
print(answer)
try:
with open('cats.txt') as f_obj_a:
content_a = f_obj_a.read()
except FileNotFoundError:
print("This file cannot be found!")
else:
print(content_a)
try:
with open('dogs.txt') as f_obj_b:
content_b = f_obj_b.read()
except FileNotFoundError:
print("This file cannot be found!")
else:
print(content_b)
try:
with open('cats.txt') as f_obj_a:
content_a = f_obj_a.read()
except FileNotFoundError:
pass
else:
print(content_a)
try:
with open('dogs.txt') as f_obj_b:
content_b = f_obj_b.read()
except FileNotFoundError:
pass
else:
print(content_b)
line = "Row,row,row your boat"
print(line.count('row'))
print(line.lower().count('row'))
with open('alice.txt') as f:
content = f.read()
print(content.lower().count('the'))