简体   繁体   English

我的 python 代码没有按预期工作并返回错误的输出

[英]my python code is not working as intended and returns wrong outputs

dacommandos = '!help'

actiontaken = input('what do you want to do: ')

if actiontaken == 'help':
    print(dacommandos)
else:
    print('no')

Whenever I type in 'help' it returns 'no', and whenever I type anything else it returns and error with input is not defined.每当我输入“帮助”时,它都会返回“否”,而每当我输入任何其他内容时,它都会返回,并且未定义输入错误。

First you need to be running this in the interpreter.首先,您需要在解释器中运行它。 So go to a command line and type in python. Then you'll see >>> which means you are in the interpreter.所以 go 到命令行并输入 python。然后你会看到 >>> 这意味着你在解释器中。

Then type each command as follows:然后按如下方式键入每个命令:

>>> dacommandos = '!help'
>>> actiontaken = input('what do you want to do: ')
what do you want to do: help
>>> if actiontaken == 'help':
...     print(dacommandos)
... else:
...     print('no')
... 
!help

Your system might be including the terminal return in the string.您的系统可能在字符串中包含终端返回。 Try printing out actiontaken to see what's in it as well as the length of the string.尝试打印 actiontaken 以查看其中的内容以及字符串的长度。 Be careful on indentation using tabs vs spaces.使用制表符和空格时要小心缩进。 You might end up testing the first four characters in actiontaken == 'help'.您可能最终会测试 actiontaken == 'help' 中的前四个字符。

dacommandos = '!help'

actiontaken = input('what do you want to do: ')

if actiontaken == 'help':
    print(dacommandos)
else:
    print('no')

Whenever I type in 'help' it returns 'no', and whenever I type anything else it returns and error with input is not defined.每当我输入“帮助”时,它都会返回“否”,而每当我输入其他任何内容时,它都会返回并且输入错误未定义。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM