Published 7月 23, 2018 by with 0 comment

習題1 - 第一個程式


用Windows的cmd, 手打(不準複製貼上)一個如下的程式碼.
書中要求不準用IDLE.
print ("Hello World!")
print ("Hello Again")
print ("I like typing this.")
print ("This is fun.")
print ('Yay! Printing.')
print ("I'd much rather you 'not'.")
print ('I "said" do not touch this.')

我用Notepad++, 然後另存新檔為ex1.py.
打開cmd, 移動到Python的檔案夾, 執行ex1.py.
C:\Users\Peter\Desktop\Python\LP3THW>python ex1.py
Hello World!
Hello Again
I like typing this.
This is fun.
Yay! Printing.
I'd much rather you 'not'.
I "said" do not touch this.

C:\Users\Peter\Desktop\Python\LP3THW>

完成!
接下來最重要的是修改該ex1.py的code.
並讓結果出現錯誤訊息.
進一步了解錯誤訊息的含義.

比如,
修改line 3的程式讓他最後少一個")".
print ("Hello World!")
print ("Hello Again")
print ("I like typing this."
print ("This is fun.")
print ('Yay! Printing.')
print ("I'd much rather you 'not'.")
print ('I "said" do not touch this.')

則程式會顯示line 4有語法錯誤, 這代表要我從line 4, 開始往上找錯, line 4找不到就往上line 3找.
C:\Users\Peter\Desktop\Python\LP3THW>python ex1.py
  File "ex1.py", line 4
    print ("This is fun")
        ^

SyntaxError: invalid syntax

C:\Users\Peter\Desktop\Python\LP3THW> 

比如,
修改line 4 讓它少一個"號.
print ("Hello World!")
print ("Hello Again")
print ("I like typing this.")
print ("This is fun.)
print ('Yay! Printing.')
print ("I'd much rather you 'not'.")

就顯示EOL(End of Line).
C:\Users\Peter\Desktop\Python\LP3THW>python ex1.py
  File "ex1.py", line 4
    print ("This is fun.)
                       ^
SyntaxError: EOL while scanning string literal

C:\Users\Peter\Desktop\Python\LP3THW> 

程式會顯示line 4有EOL(End of Line)錯, 這代表要檢查line 4.


最初發表 / 最後更新: 2018.07.23 / 2018.07.23

0 comments:

張貼留言