如何输出已输入的输入

2024-06-16 18:24:53 发布

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

编辑: 我试图提交我的任务,它说“测试问题中的第一个示例。您提交的内容试图读取太多输入!这发生在您提交的第3行。”有人知道这可能意味着什么吗?或者如何解决这个问题


我使用Python创建了这个程序。如果用户输入同一个口袋妖怪两次,我想让它说,口袋妖怪已经被捕获。(您已经在培训Eevee)

我的代码:

Pokemon = {}
while True:
  command = input("Command: ")
  if "Capture" in command:
      command = command.split(' ')
      Pokemon[command[1]] = command[2]
      print(command[2])
      print(Pokemon[command[1]])
      #if command[2] in Pokemon:
        #print("You are already training " + Pokemon[command[1]])

  if "Query" in command:
       command = command.split(' ')
       if command[1] not in Pokemon:
           print("You have not captured " +  command[1] + " yet.")
       else:
           print(command[1] + " is level " + Pokemon[command[1]] + ".") 
  else:
    print("Unknown command!")

我希望代码执行的操作:

Command: Capture Eevee 4
Command: Query Eevee
Eevee is level 4.
Command: Capture Eevee 6
You are already training Eevee!
Command: 

提前感谢:)


Tags: inyouiftrainingqueryarecommandcapture
2条回答

您可以尝试以下方法:

Pokemon = {}
while True:
    command = input("Command: ")
    if "Capture" in command:
        command = command.split(' ')
        if not command[1] in Pokemon:
            Pokemon[command[1]] = command[2]
        else:
            print("You are already training {}!".format(command[1]))
    elif "Query" in command:
        command = command.split(' ')
        if command[1] not in Pokemon:
            print("You have not captured " +  command[1] + " yet.")
        else:
            print(command[1] + " is level " + Pokemon[command[1]] + ".") 
    else:
        print("Unknown command!")

输出:

Command: Capture Eevee 4
Command: Query Eevee
Eevee is level 4.
Command: Capture Eevee 6
You are already training Eevee!
Command: 

试试这个:

var = None

if var is None:
    var = input("Command: ")
else:
    print("Already entered!")

相关问题 更多 >