如何检查raw_input == python中的某些内容?

这是我写的,请您看看我的代码有什么问题。我只是Python的初学者。


#!/usr/bin/python


input=int(raw_input("Write exit here: "))


if input==exit:

    print "Exiting!"

else:

    print "Not exiting!"


冉冉说
浏览 152回答 4
4回答

哔哔one

您想测试字符串的相等性"exit",所以不要将其转换为inttext = raw_input("Write exit here: ")if text == "exit":    print "Exiting!"else:    print "Not exiting!"input==exitinput与exit可能使您感到困惑的功能相比。如果您尝试过,input == Exit它甚至不会运行。

蓝山帝景

Python是一种脚本语言-交互式地运行python(只需键入python)或运行调试器(如idle,eric,komodo(等等))非常容易,并且可以使用它。在这里,我尝试了一些组合,以查看哪些有效,哪些无效:>>> raw_input("write exit here: ")write exit here: exit'exit'>>> my_input = raw_input("write exit here: ")write exit here: exit>>> my_input'exit'>>> my_input = int(raw_input("wite exit here: "))wite exit here: exitTraceback (most recent call last):&nbsp; File "<pyshell#11>", line 1, in <module>&nbsp; &nbsp; my_input = int(raw_input("wite exit here: "))ValueError: invalid literal for int() with base 10: 'exit'>>> my_input == exitFalse>>> type(exit)<class 'site.Quitter'>>>> my_input == "exit"True但是,请不要相信我的意思。...打开解释器,对问题的一小部分进行试验,您可以立即使用它。
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Python