【问题标题】:string MUST contain both chars and not just 1 of them字符串必须包含两个字符,而不仅仅是其中的 1 个
【发布时间】:2020-11-29 03:58:44
【问题描述】:

对你们来说可能听起来太简单了,所以我很抱歉浪费了你们的时间,但这让我可怜的大脑崩溃了。所以不管它的价值,这里是:

第一个任务没问题: 如果字符串有“i”或“u”,则打印“there is an i or a u”。

string = "demo loops for you"
for char in string:
   if char == 'i' or char == 'u':
       print("There is an i or a u")

第二个是令人心碎的,尤其是在第一个条件下:

如果字符串既有“i”又有“u”,则打印“同时存在i和u”,因此如果只有“i”但没有“u”,则打印“只有i”,如果只有“u”但没有“i”,然后打印“只有你”

第一个条件的这个块不起作用。它仍然会打印(即使只有一个字符->“i”):

string = "i can demo loops"
for char in string:
   if char == 'i' and 'u':
       print("There is both an i or u")

启发我哦,聪明的人! :'(

【问题讨论】:

  • *same character" 不能同时是 iu。(另外,char == i` 和 u 不会将该字符与 u 进行比较)跨度>
  • 谢谢你,@rici。感谢您的回复。我想我得到了你评论的第一部分。但是第二部分,我没有意识到if char == 'i' and 'u' 实际上是在与u 进行比较。伙计,我真的有很多通宵达旦。
  • if char == 'i' and 'u' 被解析为if (char == 'i') and 'u'。根据 Python 对真实性的定义,'u'true。此外,对于 b 的任一布尔值,b and trueb 相同。所以你只是在测试char == 'i'.

标签: python string boolean operators


【解决方案1】:

您可以使用两个计数变量来完成这项工作

string = "demo loops  u for yo"

icount=string.count('i')
ucount=string.count('u')


if (icount >0 and ucount==0) :
  print ("there is only i but not u")

if (icount ==0 and ucount>0):
  print ("there is only u but not i")

if (icount >0 and ucount>0):
  print ("there are both u and i")

【讨论】:

  • string.count('i') 会比循环更 Pythonic(而且更快)。
  • 我是故意这样做的,只是为了让它与 OP 的 python 级别相匹配,但我相信当他编码更多时他会进步
  • 所以不是聊天。这是一本问答百科全书。这样的答案会被别人找到,甚至会被别人抄袭。所以它的贡献并不好。您可以随意添加您认为 OP 知识水平的文本,但您仍应始终尝试写出好的答案。
  • 同意。让我稍微修改一下
  • 谢谢 rici 和 Ken。我也检查了以前的版本,它给了我更多的洞察力。哇!在发布之前,我实际上在网上寻找了类似的问题。现在我在想,也许我很难找到一个的原因是因为这对你们来说实际上是一个相对简单的问题,而且我太笨了,至少无法弄清楚发生了什么。更何况现在我意识到这个地方被像你这样的天才所包围。具有讽刺意味的是,似乎我对 python 了解得越多,我就越觉得自己很愚蠢。大声笑
【解决方案2】:

首先,你可以在字符串中同时检查“i”和“u”,然后只能一个一个地检查。

string = "i can demo loops u"

if "i" in string and "u" in string:
    print("There are both an i or u")
elif "i" in string :
    print("There is an i in string")
elif "u" in string :
    print("There is an u in string") 

【讨论】:

  • 谢谢@Sonal Savaliya。非常感谢!是的,在帖子中检查两个字符是否存在是我的想法(因此提到了“第一个条件”)。所以现在,我仍然无法停止思考为什么我以前没有想过以这种方式运行它,因为在我的水平上,这篇文章是最直接的,因为这里使用的陈述是新手应该熟悉的一些最早的陈述和。无论如何,非常感谢!
  • 不客气。我很高兴。它有帮助。别担心,有时每个人都会遇到这种情况。最简单的事情不会想到。
【解决方案3】:

由于您提供的见解,我能够制作一个“稍微好一点”的版本,它不仅可以确定存在,还可以要求输入、存储变量、计算字符并相应地打印出值,重点是单数/复数名词。如果您看到我可能遗漏的东西,或者只是对如何使这段代码更“pythonic”有任何建议,我也会非常感谢他们。再次感谢!

string = input ('enter a phrase, and i\'ll tell you how many i\'s and u\'s it has: ')
icounter = string.count('i')
ucounter = string.count('u')

if icounter == 0 and ucounter == 0:
    print ('you phrase has no i\'s or u\'s')
if icounter == 1 and ucounter == 1:
    print ('your phrase has', icounter , "i", "and", ucounter, "u")
if icounter > 1 and ucounter > 1:
    print ('your phrase has', icounter, "i's and", ucounter, "u's")
if icounter == 1 and ucounter == 0:
    print ('your phrase has an "i" but no "u"')
if icounter == 0 and ucounter == 1:
    print ('your phrase has a "u" but no "i"s')
if icounter > 1 and ucounter == 0:
    print ('your phrase has', icounter, '"i"\'s but no "u"\'s')
if icounter == 0 and ucounter > 1:
    print ('your phrase has', ucounter, '"u"\'s but no "i"\'s')
if icounter > 1 and ucounter == 1:
    print ('your phrase has', icounter, "i's and", 'a "u"')
if icounter == 1 and ucounter > 1:
    print ('your phrase has an "i"', "and", ucounter, 'u\'s')

【讨论】:

    猜你喜欢
    • 2019-02-17
    • 2013-05-25
    • 2021-09-16
    • 1970-01-01
    • 2022-11-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多