无法修复python无效语法

问题描述 投票:-1回答:3
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

基本上我的问题是上面的'无效语法'[你想玩吗? ] '没有反应。

但当我擦除它时,问题就转到了下一行。这很烦人,我找不到如何解决它。

我尝试在'和'之间切换,但结果仍然相同。

我很高兴你们中的任何一个人会花时间回应我昨天刚刚开始做的并且仍然不清楚无效语法。

python python-3.x syntax
3个回答
0
投票
if choice 'no' :

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

if choice == 'no' :

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

choice = input( "Yes or no?")

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

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

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

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


0
投票

问题:

  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()

0
投票

试试这个:

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

希望这个有效!这可能会更好。

© www.soinside.com 2019 - 2024. All rights reserved.