如何返回到python中的变量?

2024-05-19 05:51:41 发布

您现在位置:Python中文网/ 问答频道 /正文

我用Python编写这个脚本已经有一段时间了。然而,我遇到了一个问题: 在我的代码中,用户键入一个命令并显示一些文本(有点像MS-DOS),但我不知道如何让它回到问题上来。(节目刚刚结束。)

以下是我的脚本示例:

command=raw_input("j;/")

command1="help"

command2="jver"


if command==command1:

   print "List of commands"

   print""

   print""

   print"help = shows all commands"

   print ""

   print "jver = Version of MS-Josh"

   #Code here so that it goes back to the j;/ raw_input question.

elif command==command2:

     print "MS-Josh version 3.12."

     print ""

     print "memory used: 7,117 bytes"

     print "memory on computer: 1,434,078 bytes"

     print "memory free: 1,426,961"

      #Code here so that it goes back to the j;/ raw_input question.

else:
   print "incorrect command"

Tags: of脚本inputrawhelpcodecommandcommands
2条回答

使用迭代循环您的命令,并给用户退出的方式

while True:
   command=raw_input("j;/")
   command1="help"
   command2="jver"
   command3='end'

   if command == command3:
      return
   elif command==command1:
      ...... your code

也许你想要:

while True:
    ...

如果您将语句放在脚本的顶部,这将使您回到顶部。你知道吗

像这样:

while True:
   command=raw_input("j;/")
   command1="help"
   command2="jver"


   if command==command1:
      print "List of commands"
      print""
      print""
      print"help = shows all commands"
      print ""
      print "jver = Version of MS-Josh"
   elif command==command2:
        print "MS-Josh version 3.12."
        print ""
        print "memory used: 7,117 bytes"
        print "memory on computer: 1,434,078 bytes"
        print "memory free: 1,426,961"
   else:
      print "incorrect command"

相关问题 更多 >

    热门问题