10 - 1
with open('a.txt') as text:
all = text.read()
print(all)
with open('a.txt') as text:
for line in text :
print(line.rstrip())
with open('a.txt') as text:
lines = text.readlines()
print (lines)
10 - 3
with open('a.txt','w') as text:
text.write('IN\n')
text.write('PYTHON\n')
text.write('A')
10 - 6
try:
x = input("Give me a number: ")
x = int(x)
y = input("Give me another number: ")
y = int(y)
except ValueError:
print("Sorry, I really needed a number.")
else:
sum = x + y
print("The sum of " + str(x) + " and " + str(y) + " is " + str(sum) + ".")
10 - 11
import json
number = input("What's your favorite number? ")
with open('favorite_number.json', 'w') as f:
json.dump(number, f)
print("Thanks! I'll remember that.")
with open('favorite_number.json') as f:
number = json.load(f)
print("I know your favorite number! It's " + str(number) + ".")