用Notepad++打下列的程式碼,
另存為ex14.py.
from sys import argv script, user_name = argv prompt = '> ' print (f"Hi {user_name}, I'm the {script} script.") print ("I'd like to ask you a few questions.") print (f"Do you like me {user_name}?") likes = input (prompt) print (f"Where do you live {user_name}?") lives = input (prompt) print ("What kind of computer do you have?") computer = input (prompt) print (f""" Alright, so you said {likes} about liking me. You live in {lives}, Not sure where that is. And you have a {computer} computer. Nice """)
然後用Windows的cmd, 執行python打開它. (並在後面加了1個參數: Peter)
C:\Windows\System32>cd C:\Users\Peter\Desktop\Python\LP3THW C:\Users\Peter\Desktop\Python\LP3THW>python .\ex14.py Peter Hi Peter, I'm the .\ex14.py script. I'd like to ask you a few questions. Do you like me Peter? > Yes Where do you live Peter? > China What kind of computer do you have? > ThinkPad T430s Alright, so you said Yes about liking me. You live in China, Not sure where that is. And you have a ThinkPad T430s computer. Nice C:\Users\Peter\Desktop\Python\LP3THW>
完成
我們來修改一下程式碼.
Line 4, 使用格式化"f"讓提示字符更多樣化.
Line 9, 讓這likes輸入必須是字浮串, 若輸入數字會出錯.
Line 10, 印出出這likes的形態, 這招很重要!!
Line 11, 印出這likes 的string格式的值, 這招很重要!!
from sys import argv script, user_name = argv prompt = f'{script}@{user_name}> ' print (f"Hi {user_name}, I'm the {script} script.") print ("I'd like to ask you a few questions.") print (f"Do you like me {user_name}?") likes = int(input (prompt)) print (">>>> type >>>>", type (likes)) print (">>>> repr >>>>", type (repr (likes))) print (f"Where do you live {user_name}?") lives = input (prompt) print ("What kind of computer do you have?") computer = input (prompt) print (f""" Alright, so you said {likes} about liking me. You live in {lives}, Not sure where that is. And you have a {computer} computer. Nice """)
結果,
C:\Users\Peter\Desktop\Python\LP3THW>python ex14.py Peter Hi Peter, I'm the ex14.py script. I'd like to ask you a few questions. Do you like me Peter? ex14.py@Peter> 111 >>>> type >>>> <class 'int'> >>>> repr >>>> <class 'str'> Where do you live Peter? ex14.py@Peter> AAA What kind of computer do you have? ex14.py@Peter> BB1B Alright, so you said 111 about liking me. You live in AAA, Not sure where that is. And you have a BB1B computer. Nice C:\Users\Peter\Desktop\Python\LP3THW>
最初發表 / 最後更新: 2018.08.14 / 2018.08.14
0 comments:
張貼留言