File tree Expand file tree Collapse file tree 17 files changed +194
-0
lines changed
Expand file tree Collapse file tree 17 files changed +194
-0
lines changed Original file line number Diff line number Diff line change 1+ # encoding: utf-8
2+
3+ filename = 'learning_python.txt'
4+
5+ # 第一次打印读取整个文件
6+ print ("--- Reading in the entire file:" )
7+ with open (filename ) as f :
8+ contents = f .read ()
9+ print (contents )
10+
11+ # 第二次打印时遍历文件对象
12+ print ("\n --- Looping over the lines:" )
13+ with open (filename ) as f :
14+ for line in f :
15+ print (line .rstrip ())
16+
17+ # 第三次打印时将各行存储在一个列表中,再在with代码块外打印它们
18+ print ("\n --- Storing the lines in a list:" )
19+ with open (filename ) as f :
20+ lines = f .readlines ()
21+
22+ for line in lines :
23+ print (line .rstrip ())
Original file line number Diff line number Diff line change 1+ import json
2+
3+ with open ('favorite_number.json' ) as f :
4+ number = json .load (f )
5+
6+ print ("I know your favorite number! It's " + str (number ) + "." )
Original file line number Diff line number Diff line change 1+ import json
2+
3+ number = input ("What's your favorite number? " )
4+
5+ with open ('favorite_number.json' , 'w' ) as f :
6+ json .dump (number , f )
7+ print ("Thanks! I'll remember that." )
Original file line number Diff line number Diff line change 1+ import json
2+
3+ try :
4+ with open ('favorite_number.json' ) as f :
5+ number = json .load (f )
6+ except FileNotFoundError :
7+ number = input ("What's your favorite number? " )
8+ with open ('favorite_number.json' , 'w' ) as f :
9+ json .dump (number , f )
10+ print ("Thanks, I'll remember that." )
11+ else :
12+ print ("I know your favorite number! It's " + str (number ) + "." )
Original file line number Diff line number Diff line change 1+ import json
2+
3+ def get_stored_username ():
4+ """Get stored username if available."""
5+ filename = 'username.json'
6+ try :
7+ with open (filename ) as f_obj :
8+ username = json .load (f_obj )
9+ except FileNotFoundError :
10+ return None
11+ else :
12+ return username
13+
14+ def get_new_username ():
15+ """Prompt for a new username."""
16+ username = input ("What is your name? " )
17+ filename = 'username.json'
18+ with open (filename , 'w' ) as f_obj :
19+ json .dump (username , f_obj )
20+ return username
21+
22+ def greet_user ():
23+ """Greet the user by name."""
24+ username = get_stored_username ()
25+ if username :
26+ correct = input ("Are you " + username + "? (y/n) " )
27+ if correct == 'y' :
28+ print ("Welcome back, " + username + "!" )
29+ else :
30+ username = get_new_username ()
31+ print ("We'll remember you when you come back, " + username + "!" )
32+ else :
33+ username = get_new_username ()
34+ print ("We'll remember you when you come back, " + username + "!" )
35+
36+ greet_user ()
Original file line number Diff line number Diff line change 1+ # encoding: utf-8
2+
3+ filename = './learning_python.txt'
4+
5+ with open (filename ) as f :
6+ lines = f .readlines ()
7+
8+ for line in lines :
9+ line = line .rstrip ()
10+ print (line .replace ('Python' , 'C' ))
Original file line number Diff line number Diff line change 1+ name = input ("What's your name? " )
2+
3+ filename = 'guest.txt'
4+
5+ with open (filename , 'w' ) as f :
6+ f .write (name )
Original file line number Diff line number Diff line change 1+ filename = 'guest_book.txt'
2+
3+ print ("Enter 'quit' when you are finished." )
4+ while True :
5+ name = input ("\n What's your name? " )
6+ if name == 'quit' :
7+ break
8+ else :
9+ with open (filename , 'a' ) as f :
10+ f .write (name + "\n " )
11+ print ("Hi " + name + ", you've been added to the guest book." )
Original file line number Diff line number Diff line change 1+ filename = 'programming_poll.txt'
2+
3+ responses = []
4+ while True :
5+ response = input ("\n Why do you like programming? " )
6+ responses .append (response )
7+
8+ continue_poll = input ("Would you like to let someone else respond? (y/n) " )
9+ if continue_poll != 'y' :
10+ break
11+
12+ with open (filename , 'a' ) as f :
13+ for response in responses :
14+ f .write (response + "\n " )
Original file line number Diff line number Diff line change 1+ try :
2+ x = input ("Give me a number: " )
3+ x = int (x )
4+
5+ y = input ("Give me another number: " )
6+ y = int (y )
7+
8+ except ValueError :
9+ print ("Sorry, I really needed a number." )
10+
11+ else :
12+ sum = x + y
13+ print ("The sum of " + str (x ) + " and " + str (y ) + " is " + str (sum ) + "." )
You can’t perform that action at this time.
0 commit comments