교재 제10장 수업 후 연습문제(부분)

2127 단어 python 학습
코드:
#10.1
print(10.1)
filename = 'test.txt'

print("    ")
with open(filename) as file:
	contents = file.read();
	print(contents)
print("       ")
sentences = []
with open(filename) as file:
	for line in file:
		sentences.append(line.rstrip())
for each in sentences:
	print(each)
print("    ")
lines = []
with open(filename) as file:
	lines = file.readlines()
for line in lines:
	print(line.rstrip())

#10.2
print(10.2)
lines2 = []
with open(filename) as file:
	lines2 = file.readlines()
for line in lines2:
	line = line.replace('Python','C++').rstrip()
	print(line)

#10.3
print(10.3)
filename = 'guest.txt'
with open(filename,'w') as file:
	str1 = input("Please input your name:")
	file.write(str1)
with open(filename) as file:
	print(file.read())

#10.4
print(10.4)
with open(filename,'w') as file:
	str1 = input("Your name:")
	while(str1 != 'quit'):
		file.write(str1+'
') str1 = input("Your name:") with open(filename) as file: print(file.read().rstrip()) #10.6 print(10.6) try: num1 = int(input("num1:")) num2 = int(input("num2:")) except ValueError: print("ValueError") else: print(num1+num2) print(10.7) while True: try: num1 = input("num1:") if(num1 == 'q'): break num1 = int(num1) num2 = input("num2:") if(num2 == 'q'): break num2 = int(num2) except ValueError: print("ValueError,try again.") else: print(num1+num2)

출력:
10.1
    
In Python you can play
In Python you can code
In Python you can debug
       
In Python you can play
In Python you can code
In Python you can debug
    
In Python you can play
In Python you can code
In Python you can debug
10.2
In C++ you can play
In C++ you can code
In C++ you can debug
10.3
Please input your name:jack
jack
10.4
Your name:pig
Your name:zet
Your name:quit
pig
zet
10.6
num1:1
num2:2
3
10.7
num1:3
num2:4
7
num1:5
num2:6
11
num1:k
ValueError,try again.
num1:5
num2:k
ValueError,try again.
num1:q

좋은 웹페이지 즐겨찾기