我一直試圖做一個簡單的猜數字遊戲,但Python一直說我的代碼有語法錯誤。我究竟做錯了什麼?包含if語句的語句錯誤
import random
takeone = input("Guess A Number Between 1 & 5")
numberinit = random.randint(1,5)
if "takeone" == "numberinit"
print("Your Right")
else:
print("Your Wrong")
我一直試圖做一個簡單的猜數字遊戲,但Python一直說我的代碼有語法錯誤。我究竟做錯了什麼?包含if語句的語句錯誤
import random
takeone = input("Guess A Number Between 1 & 5")
numberinit = random.randint(1,5)
if "takeone" == "numberinit"
print("Your Right")
else:
print("Your Wrong")
你得到的語法錯誤可能是類似以下內容:
File "test.py", line 4
if "takeone" == "numberinit"
^
SyntaxError: invalid syntax
這意味着你錯過了在if
行的末尾有一個冒號。該行應改爲閱讀:蟒if
聲明
if "takeone" == "numberinit":
冒號不分號 –
@tourniquet_grab謝謝!愚蠢的錯誤。糾正。 – jotik
文檔可以在https://docs.python.org/2/tutorial/controlflow.html找到。
這裏有一個正確的示例:
import random
takeone = input("Guess A Number Between 1 & 5")
numberinit = random.randint(1,5)
if "takeone" == "numberinit":
print("Your Right")
else:
print("Your Wrong")
蟒蛇基礎...'如果:' –