1. 程式人生 > >python習題14

python習題14

 1 #引入特性
 2 from sys import argv
 3 
 4 #解包
 5 script, user_name = argv
 6 #把使用者提示符>(字串)賦值給prompt
 7 prompt = '>'
 8 
 9 #把變數user_name、script帶入並且列印整句話
10 print(f"Hi {user_name}, I'm the {script} script.")
11 #列印
12 print("I'd like to ask you a few questions.")
13 #嵌入並且列印
14 print(f"Do you like me {user_name}?
") 15 #首先會出現一個>,再輸入內容 16 likes = input(prompt) 17 18 print(f"Where do you live {user_name}?") 19 lives = input(prompt) 20 21 print("What kind of computer do you have?") 22 computer = input(prompt) 23 24 #把之前輸入的變數嵌入到字串裡,並且列印 25 print(f""" 26 Alright, so you said {likes} about liking me. 27 You live in {lives}. Not sure where that is.
28 And you have a {computer} computer. Nice. 29 """)

 

將prompt變數改成完全不同的內容再執行一遍

 1 #引入特性
 2 from sys import argv
 3 
 4 #解包
 5 script, user_name = argv
 6 #把使用者提示符>(字串)賦值給prompt
 7 prompt = '我來回答一下:'
 8 
 9 #把變數user_name、script帶入並且列印整句話
10 print(f"Hi {user_name}, I'm the {script} script.
") 11 #列印 12 print("I'd like to ask you a few questions.") 13 #嵌入並且列印 14 print(f"Do you like me {user_name}?") 15 #首先會出現一個>,再輸入內容 16 likes = input(prompt) 17 18 print(f"Where do you live {user_name}?") 19 lives = input(prompt) 20 21 print("What kind of computer do you have?") 22 computer = input(prompt) 23 24 #分行列印 25 print(f""" 26 Alright, so you said {likes} about liking me. 27 You live in {lives}. Not sure where that is. 28 And you have a {computer} computer. Nice. 29 """)

 

將你的指令碼再新增一個引數,並且使用這個引數,格式和ex13.py的first,second = argv 一樣

 1 #引入特性
 2 from sys import argv
 3 
 4 #解包模組
 5 script, user_name ,age= argv
 6 #把使用者提示符>(字串)賦值給prompt
 7 prompt = '我來回答一下:'
 8 
 9 #把變數user_name、script帶入並且列印整句話
10 print(f"Hi {user_name}, I'm the {script} script,you are {age} old.")
11 #列印
12 print("I'd like to ask you a few questions.")
13 print("Your age is: ",age)
14 #嵌入並且列印
15 print(f"Do you like me {user_name}?")
16 #首先會出現一個>,再輸入內容
17 likes = input(prompt)
18 
19 print(f"Where do you live {user_name}?")
20 lives = input(prompt)
21 
22 print("What kind of computer do you have?")
23 computer = input(prompt)
24 
25 #分行列印
26 print(f"""
27 Alright, so you said {likes} about liking me.
28 You live in {lives}. Not sure where that is.
29 And you have a {computer} computer. Nice.
30 """)