簡體   English   中英

Python:制作魔術8球游戲,為什么我不斷收到語法錯誤? 這是代碼,如果需要,請對其進行測試

[英]Python: Making a Magic 8 Ball game, why do i keep getting syntax errors? Here's the code, test it if you want

這是我的代碼。 當我問一個問題時,出現SyntaxError:無效的語法。 任何幫助,將不勝感激。

import random

answer1=("Absolutely!")
answer2=("No way Pedro!")
answer3=("Go for it tiger.")

print ("Welcome to the Magic 8 Ball game - use it to answer your questions...")

question = input("Ask me for any advice and I'll help you out. Type in your question and then press enter for an answer.")

print ("shaking....\n"*4)

choice=random.randint(1,3)

if choice == 1:
    **strong text**answer==answer1
elif choice == 2:
    answer==answer2
else:
    answer==answer3

print (answer)

當我問任何問題時出現此錯誤

Welcome to the Magic 8 Ball game - use it to answer your questions...
Ask me for any advice and I'll help you out. Type in your question and then press enter for an answer.should i play xbox
Traceback (most recent call last):
  File "E:\Magic8Ball.py", line 11, in <module>
    question = input("Ask me for any advice and I'll help you out. Type in your question and then press enter for an answer.")
  File "<string>", line 1
    should i play xbox
           ^
SyntaxError: invalid syntax
>>>

您正在使用== ,這是一個比較運算符來分配變量。 應該改為= 另外,您忘了聲明答案: answer = ""

import random

answer1=("Absolutely!")
answer2=("No way Pedro!")
answer3=("Go for it tiger.")

print ("Welcome to the Magic 8 Ball game - use it to answer your questions...")

question = input("Ask me for any advice and I'll help you out. Type in your question and then press enter for an answer.")

print ("shaking....\n"*4)

choice=random.randint(1,3)
answer = ""
if choice == 1:
    answer=answer1
elif choice == 2:
    answer=answer2
else:
    answer=answer3

print (answer)

如果您使用的是Python2.7,請使用raw_input代替input

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM