字符串必须包含两个字符,而不仅仅是其中的一个
string MUST contain both chars and not just 1 of them
可能听起来对你们来说太简单了,所以我很抱歉浪费你们的时间,但它让我可怜的大脑崩溃了。因此,就其价值而言,它是这样的:
第一个任务没问题:
如果字符串有一个“i”或一个“u”,打印“有一个 i 或一个 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”,则打印“只有 u”
第一个条件的此块不起作用。它仍然打印(即使只有一个字符 - >“i”):
string = "i can demo loops"
for char in string:
if char == 'i' and 'u':
print("There is both an i or u")
开导我哦,聪明人! :'(
您可以使用两个计数变量来完成这项工作
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")
首先,您可以检查字符串中的“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")
由于您提供的见解,我能够制作一个“稍微好一点”的版本,它不仅可以确定存在性,还可以要求输入、存储变量、计算字符数并相应地重点打印出值关于 singular/plural 名词。如果您看到我可能遗漏的内容,或者对如何使这段代码更“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')
可能听起来对你们来说太简单了,所以我很抱歉浪费你们的时间,但它让我可怜的大脑崩溃了。因此,就其价值而言,它是这样的:
第一个任务没问题: 如果字符串有一个“i”或一个“u”,打印“有一个 i 或一个 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”,则打印“只有 u”
第一个条件的此块不起作用。它仍然打印(即使只有一个字符 - >“i”):
string = "i can demo loops"
for char in string:
if char == 'i' and 'u':
print("There is both an i or u")
开导我哦,聪明人! :'(
您可以使用两个计数变量来完成这项工作
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")
首先,您可以检查字符串中的“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")
由于您提供的见解,我能够制作一个“稍微好一点”的版本,它不仅可以确定存在性,还可以要求输入、存储变量、计算字符数并相应地重点打印出值关于 singular/plural 名词。如果您看到我可能遗漏的内容,或者对如何使这段代码更“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')