如果已经问过这个问题,我很抱歉,但是我已经搜索过(在互联网和堆栈流程上)并且无法找到我的问题的解决方案。我刚刚开始学习python编程,并且遇到了一些问题。我一直在尝试将以下算法转换为python代码:
main ()
name = getPlayername
menuChoice = ‘’
get menuChoice ! = ‘Q’
display menu
if menuChoice = ‘p’ or ‘P’
display Start Game message “Let’s play some golf!”
playGame ()
otherwise, if menuChoice = ‘i’ or ‘I’
display instructions message
otherwise, if menuChoice = ‘Q’ or ‘q”
display End message
Otherwise if menuChoice ≠ ‘P’ or ‘I’ or ‘Q’
display Invalid Selection message
基本上,我希望玩家输入ether P,I或Q,具体取决于他们想要做什么,然后跟进命令(无论是显示消息还是继续使用其他功能)。以下是我目前在Python中使用的代码:
def main (playersName):
getMenuChoice = input ("""
Please select one of the following options:
Play Game
Instructions
Quit
""")
menuChoice = ['P', 'p', 'Q', 'q', 'I', 'i']
if menuChoice is p or P:
print("Let's play some golf!")
elif menuChoice is i or I:
print("This is a simple golf game in which each hole is 230m game away with par 5. You’re able to choose from 3 clubs, the Driver, Iron or Putter. The Driver will hit around 100m, the Iron around 30m and the Putter around 10m. The Putter is best used very close to the hole.")
elif menuChoice is Q or q:
print ("Farewell and thank-you for playing" + str (playersName))
else:
print ("Invalid Command")
getMenuChoice = input ("""
Please select one of the following options:
Play Game
Instructions
Quit
""")
然而,当我运行代码时,我总是遇到问题;无效的语法错误告诉我=符号,或者:或'符号是问题,并且声明说'if menuChoice是p或P:'中的'P'未定义,即使我已经定义了它列表作为menuChoice的值。在python中查看有关循环和列表的不同网站之后,我的所有代码看起来都是正确的,但python并不这么认为。任何人都可以帮助我,因为我不知道我做错了什么。