为了账号安全,请及时绑定邮箱和手机立即绑定

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

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

冉冉说 2021-03-21 15:11:57
这是我写的,请您看看我的代码有什么问题。我只是Python的初学者。#!/usr/bin/pythoninput=int(raw_input("Write exit here: "))if input==exit:    print "Exiting!"else:    print "Not exiting!"
查看完整描述

4 回答

?
哔哔one

TA贡献1854条经验 获得超8个赞

您想测试字符串的相等性"exit",所以不要将其转换为int


text = raw_input("Write exit here: ")


if text == "exit":

    print "Exiting!"

else:

    print "Not exiting!"

input==exitinput与exit可能使您感到困惑的功能相比。如果您尝试过,input == Exit它甚至不会运行。


查看完整回答
反对 回复 2021-03-31
?
蓝山帝景

TA贡献1843条经验 获得超7个赞

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: exit


Traceback (most recent call last):

  File "<pyshell#11>", line 1, in <module>

    my_input = int(raw_input("wite exit here: "))

ValueError: invalid literal for int() with base 10: 'exit'


>>> my_input == exit

False

>>> type(exit)

<class 'site.Quitter'>

>>> my_input == "exit"

True

但是,请不要相信我的意思。...打开解释器,对问题的一小部分进行试验,您可以立即使用它。


查看完整回答
反对 回复 2021-03-31
  • 4 回答
  • 0 关注
  • 151 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
微信客服

购课补贴
联系客服咨询优惠详情

帮助反馈 APP下载

慕课网APP
您的移动学习伙伴

公众号

扫描二维码
关注慕课网微信公众号