10-1
with open('learning_python.txt') as file:
print(file.read())
with open('learning_python.txt') as file:
for line in file:
print(line.rstrip())
with open('learning_python.txt') as file:
leaning = file.readlines()
print(leaning)
10-2
with open('learning_python.txt') as file:
for line in file:
print(line.rstrip().replace('Python', 'C++'))
10-3
with open('guest.txt', 'w') as file:
In = input("Input your name('q' to quit):")
while In != 'q':
file.write(In + '\n')
In = input("Input your name('q' to quit):")
10-7
print("Give me two numbers, and I'll add them.")
print("Enter 'q' to quit.")
while True:
fnum = input("\nFirst number: ")
if fnum == 'q':
break
snum = input("Second number: ")
if snum == 'q':
break
try:
print(int(fnum) + int(snum))
except ValueError:
print("Invalid number!")
continue
10-11
import json
lists = []
while True:
num = input("Enter your favourate number('q' to quit): ")
if num == 'q':
break
lists.append(int(num))
with open('favourate_number.json', 'w') as file:
json.dump(lists, file)
import json
with open('favourate_number.json', 'r') as file:
nums = json.load(file)
print("I know your favourite number! It's", end=' ')
for num in nums:
print(num, end=' ')