Python: 制作了一个 Magic 8 Ball 游戏,为什么我总是出现语法错误?代码如下,有需要的可以试试
Python: Making a Magic 8 Ball game, why do i keep getting syntax errors? Here's the code, test it if you want
这是我的代码。当我问一个问题时,我得到一个 SyntaxError: invalid syntax。任何帮助将不胜感激。
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
这是我的代码。当我问一个问题时,我得到一个 SyntaxError: invalid syntax。任何帮助将不胜感激。
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