【问题标题】:Cannot fix python invalid syntax无法修复python无效语法
【发布时间】:2017-12-21 03:25:59
【问题描述】:
import time
print("hello, do you want to play this detective game?")
choice == input( "Yes or no?")
if choice 'no' :
    print: "ok then, bye"
    exit

if choice'yes':
    start
    name == input("Enter your name")
    print("hello, " + name ", in this game you are a famous detective handling a theft case."
    time.sleep(1)
    print(" you have gone on a holiday but ended up lost in the forest. you see a man walking up to you")
    time.sleep(1)
    print ("man:' hi there! you seem quite lost! Do you want to stay the night with me?'")
    print(" you said:'why should i? i dont even know you'")
    print(" man : ' Do as you please, but if you stay in the forest for the night here's a friendly warning: there's wild wolves in the forests.'")
    choose == input( "stay the night or refuse?" )
    if choose "refuse":
          print("man: ' boy, you are stubborn!")
          print( "the man walks away, leaving you behind" + " In the end, you got eaten by the wolves due to your stubborness. THE END")
          choice == input("do you want to play again? yes or no?")
          if choice "yes":
          start
          if choice "no":
          print( "ok then, bye!")
          exit

基本上我的问题是上面的“无效语法”[你想玩吗? ] '不' 回应。

但是当我删除它时,问题转到下一行。这很烦人,我找不到解决方法。

我尝试在 ' 和 " 之间进行更改,但结果仍然相同。

如果你们中的任何人愿意花时间回复我,我昨天刚开始,但对无效语法仍然不是很清楚,我会很高兴。

【问题讨论】:

  • 你需要在这条线上加上一个右括号吗? print("hello, " + name ", in this game you are a famous detective handling a theft case." - 为什么这不像您的其他打印语句? print: "ok then, bye" --- 基本上你需要很多在输入准确你要复制的内容时更加小心。关闭还不够好。你需要准确。
  • 太多错误的语法我会推荐pycharm或任何其他IDE
  • 您的代码有许多非常基本的错误。有些行远非 Python。抱歉,我想您将不得不再次阅读本教程。

标签: python python-3.x syntax


【解决方案1】:
if choice 'no' :

是罪魁祸首(第 8 行和其他地方的问题相同)。该表达式需要 == 来制作:

if choice == 'no' :

但是,第 3 行会在您到达那里之前引起问题 - choice 未定义。 == 是一个逻辑运算符,它并不意味着赋值,因为此时代码似乎需要这样做。将其更改为使用= 执行分配:

choice = input( "Yes or no?")

另外,最好针对小写进行测试,因为您不知道用户可能输入的大小写字符的组合:

if choice.lower() == 'no' :

第 9 行再次读取 start -。也许你的意思是作为评论?如果是这样,请在行首添加'#'

这些问题只是冰山一角。或许您应该阅读一些教程和以小迭代增量编写的代码来构建您的程序。

【讨论】:

  • 这个答案甚至没有接近解决代码中的所有语法错误和其他问题。肯定有十几个。
  • @ekhumoro:总得从某个地方开始 :)
  • 我认为 OP 需要从学习如何阅读回溯开始。如果他们试图通过删除任何引发异常的行来“解决”问题,那么他们将不会留下太多代码(:
【解决方案2】:

问题:

  1. choice == input() 是错误的,让它choice = input()
  2. choice 'no' : 又在很多地方出错了choice == 'no'
  3. start/exit 不是必需的
  4. print() 函数必须有右括号

固定代码:

import time
def my_game():
    print("hello, do you want to play this detective game?")
    choice = input( "Yes or no?")
    if choice == 'no' :
        print("ok then, bye")

    if choice == 'yes':
        name = input("Enter your name")
        print("hello, " + name + ", in this game you are a famous detective handling a theft case.")
        time.sleep(1)
        print(" you have gone on a holiday but ended up lost in the forest. you see a man walking up to you")
        time.sleep(1)
        print ("man:' hi there! you seem quite lost! Do you want to stay the night with me?'")
        print(" you said:'why should i? i dont even know you'")
        print(" man : ' Do as you please, but if you stay in the forest for the night here's a friendly warning: there's wild wolves in the forests.'")
        choose = input( "stay the night or refuse?" )
        if choose == "refuse":
            print("man: ' boy, you are stubborn!")
            print( "the man walks away, leaving you behind" + " In the end, you got eaten by the wolves due to your stubborness. THE END")
            choice = input("do you want to play again? yes or no?")
            if choice == "yes":
                my_game()

            if choice == "no":
                print( "ok then, bye!")


if __name__ == "__main__":
    my_game()

【讨论】:

    【解决方案3】:

    试试这个:

    import time
    
    def game()
    
         print("hello, do you want to play this detective game?")
    
         choice == input( "Yes or no?")
    
         if choice == 'no' :
    
             print("ok then, bye")
    
             break
         if choice == 'yes':
    
             name == input("Enter your name")
    
             print("hello, " + name ", in this game you are a famous detective handling a theft case."
             time.sleep(1)
             print(" you have gone on a holiday but ended up lost in the forest. you see a man walking up to you")
             time.sleep(1)
             print ("man:' hi there! you seem quite lost! Do you want to stay the night with me?'")
             print(" you said:'why should i? i dont even know you'")
             print(" man : ' Do as you please, but if you stay in the forest for the night here's a friendly warning: there's wild wolves in the forests.'")
             choose == input( "stay the night or refuse?" )
             if choose == "refuse":
                   print("man: ' boy, you are stubborn!")
                   print( "the man walks away, leaving you behind" + " In the end, you got eaten by the wolves due to your stubborness. THE END")
    
                   choice == input("do you want to play again? yes or no?")
                   if choice == "yes":
                        game()
                   if choice == "no":
                        print("ok then, bye!")
                        break
             else:
                   #continue your game here
    

    希望这行得通!这可能会更好。

    【讨论】:

    • 一般来说,如果答案包含对代码的用途的解释,以及为什么在不介绍其他人的情况下解决问题的原因,答案会更有帮助。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-12-06
    • 2011-11-26
    相关资源
    最近更新 更多