Python 如何输出已输入的输入
编辑: 我试图提交我的任务,它说“测试问题中的第一个示例。您提交的内容试图读取太多输入!这发生在您提交的第3行。”有人知道这可能意味着什么吗?或者如何解决这个问题Python 如何输出已输入的输入,python,Python,编辑: 我试图提交我的任务,它说“测试问题中的第一个示例。您提交的内容试图读取太多输入!这发生在您提交的第3行。”有人知道这可能意味着什么吗?或者如何解决这个问题 我使用Python创建了这个程序。如果用户输入同一个口袋妖怪两次,我想让它说,口袋妖怪已经被捕获。(您已经在培训Eevee) 我的代码: Pokemon = {} while True: command = input("Command: ") if "Capture" in command: command =
我使用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:
提前感谢:)您可以尝试以下方法:
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!")
你好。下面的答案应该是正确的,我只是在这里给你的代码一个小的改进。您可以使用不带内部字符串的“command.split()”,而不是使用“command.split(“”)”。默认情况下,split()命令在空白处拆分字符串。祝你今天愉快:)