【问题标题】:Trying to make a Boolean the Word True or False尝试使单词 True 或 False 成为布尔值
【发布时间】:2017-11-30 12:18:32
【问题描述】:

我的简单 Python 程序运行正常——它打印 -1 表示 false——但我如何让它打印单词 False 来代替 -1?

import sys
text= sys.argv[1]
red= "red"
# Write your code here

def isred():
  print(text.find(red))

isred()

它打印 -1 但需要它打印“True”或“False”。

【问题讨论】:

  • 正如约翰所说,in 更简单,而且速度更快。只有在需要获取子字符串的索引时才使用.find

标签: python boolean


【解决方案1】:
print(text.find(red) >= 0)

或者,更简单:

print(red in text)

【讨论】:

  • 工作!!!!这是我错过的 Python 的一个函数 >= 0 返回单词 true 还是 false?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-08-17
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多