我不知道如何去掉这一行的结尾
I don't know how to strip the end of this line
我要在一个函数中输入两个数字,它会在你输入的两个数字之间输出一串奇数。一切似乎都很好,但我不知道如何正确地剥离末端。
# getOdds.py
# A function that receives two integers and returns a string of consecutive even numbers between the two integers.
# Define the getOdds() functions
def getOdds(p, q):
""" returns a string of odd numbers between integers p & q """
# Define odd number string
x = p
while x <= q:
if x % 2 == 0:
x += 1
# Calculate & print string
else:
print(x, end = ", ")
str(x).strip(", ")
x += 2
我只希望最后一个数字的末尾没有“,”(例如 1, 2, 3 而不是 1, 2, 3, )。
为什么 strip() 在这种情况下不起作用?我应该怎么做?
首先,str.strip
不会就地运行,您不会对新的返回值执行任何操作。其次,str.strip
不适用于您已经打印到控制台的内容。相反,检查循环是否处于最后一次迭代,并且不要在该点打印逗号。
else:
print(x, end = "")
x += 2
if x <= q: #if the while loop is still going to continue
print(', ',end="")
else: #end of the function, finish the line
print()
或者您可以使用单个 print()
调用和 range()
函数:
def getOdds(p, q):
print(*range(p if p%2 else p+1, q+1, 2), sep=', ')
结果:
>>> getOdds(3, 10)
3, 5, 7, 9
>>> getOdds(3, 11)
3, 5, 7, 9, 11
请注意,正如@TadhgMcDonald-Jensen 在下面的评论中所说,p if p%2 else p+1
可以替换为 p|1
,它使用按位或运算产生相同的结果。
不要在循环中打印数字,累积并 return 它们。即
def odds(q, p):
q += (2 if q % 2 else 1)
return range(q, p, 2)
my_odds = (str(n) for n in odds(1, 9))
print(", ".join(my_odds))
阻止 ,
出现在最后一个元素的唯一方法是防止 end=", "
出现在最后一个打印件上,有几种方法可以解决这个问题,但是在我看来,最简单的方法是获得一系列你想要打印的东西,然后一次性完成,打印功能在这方面做得很好:
>>> print(*range(5), sep=", ")
0, 1, 2, 3, 4
您可以通过将自己的函数放入辅助生成器函数中来使您自己的函数以这种方式运行,而不是 print(x,end=", ")
您只需 yield x
:
def getOdds_gen_helper(p, q):
""" generates odd numbers between integers p & q """
# Define odd number string
x = p
while x <= q:
if x % 2 == 0:
x += 1
# Calculate & print string
else:
yield x #!!! yield the values
str(x).strip(", ")
x += 2
然后您的实际 getOdds
函数将像上面的打印语句一样简单地调用助手:
def getOdds(p,q):
""" prints all of the odd numbers between integers p & q """
print(*getOdds_gen_helper(p,q), sep=", ")
请注意,您的原始文档字符串实际上是 "returns .." 但实际上 return 什么都没有,如果您想 return 这个字符串而不是打印它,那同样容易生成器和 str.join
方法:
def getOdds(p,q):
""" returns a string of odd numbers between integers p & q """
return ", ".join(str(p) for p in getOdds_gen_helper(p, q))
我要在一个函数中输入两个数字,它会在你输入的两个数字之间输出一串奇数。一切似乎都很好,但我不知道如何正确地剥离末端。
# getOdds.py
# A function that receives two integers and returns a string of consecutive even numbers between the two integers.
# Define the getOdds() functions
def getOdds(p, q):
""" returns a string of odd numbers between integers p & q """
# Define odd number string
x = p
while x <= q:
if x % 2 == 0:
x += 1
# Calculate & print string
else:
print(x, end = ", ")
str(x).strip(", ")
x += 2
我只希望最后一个数字的末尾没有“,”(例如 1, 2, 3 而不是 1, 2, 3, )。
为什么 strip() 在这种情况下不起作用?我应该怎么做?
首先,str.strip
不会就地运行,您不会对新的返回值执行任何操作。其次,str.strip
不适用于您已经打印到控制台的内容。相反,检查循环是否处于最后一次迭代,并且不要在该点打印逗号。
else:
print(x, end = "")
x += 2
if x <= q: #if the while loop is still going to continue
print(', ',end="")
else: #end of the function, finish the line
print()
或者您可以使用单个 print()
调用和 range()
函数:
def getOdds(p, q):
print(*range(p if p%2 else p+1, q+1, 2), sep=', ')
结果:
>>> getOdds(3, 10)
3, 5, 7, 9
>>> getOdds(3, 11)
3, 5, 7, 9, 11
请注意,正如@TadhgMcDonald-Jensen 在下面的评论中所说,p if p%2 else p+1
可以替换为 p|1
,它使用按位或运算产生相同的结果。
不要在循环中打印数字,累积并 return 它们。即
def odds(q, p):
q += (2 if q % 2 else 1)
return range(q, p, 2)
my_odds = (str(n) for n in odds(1, 9))
print(", ".join(my_odds))
阻止 ,
出现在最后一个元素的唯一方法是防止 end=", "
出现在最后一个打印件上,有几种方法可以解决这个问题,但是在我看来,最简单的方法是获得一系列你想要打印的东西,然后一次性完成,打印功能在这方面做得很好:
>>> print(*range(5), sep=", ")
0, 1, 2, 3, 4
您可以通过将自己的函数放入辅助生成器函数中来使您自己的函数以这种方式运行,而不是 print(x,end=", ")
您只需 yield x
:
def getOdds_gen_helper(p, q):
""" generates odd numbers between integers p & q """
# Define odd number string
x = p
while x <= q:
if x % 2 == 0:
x += 1
# Calculate & print string
else:
yield x #!!! yield the values
str(x).strip(", ")
x += 2
然后您的实际 getOdds
函数将像上面的打印语句一样简单地调用助手:
def getOdds(p,q):
""" prints all of the odd numbers between integers p & q """
print(*getOdds_gen_helper(p,q), sep=", ")
请注意,您的原始文档字符串实际上是 "returns .." 但实际上 return 什么都没有,如果您想 return 这个字符串而不是打印它,那同样容易生成器和 str.join
方法:
def getOdds(p,q):
""" returns a string of odd numbers between integers p & q """
return ", ".join(str(p) for p in getOdds_gen_helper(p, q))