Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 尝试打印从0到x的范围,用一个单词替换偶数数字,最后一个数字却没有_Python_Python 3.x - Fatal编程技术网

Python 尝试打印从0到x的范围,用一个单词替换偶数数字,最后一个数字却没有

Python 尝试打印从0到x的范围,用一个单词替换偶数数字,最后一个数字却没有,python,python-3.x,Python,Python 3.x,我是编程新手,正在参加一个关于Python的在线学校课程,作业是从0数到一个数字(x),但跳过0,数字之间没有空格或线条,还用单词“西红柿”替换偶数 正在寻找类似以下内容的答案:01tomato3tomato5tomato7tomato9tomato 说明中说不要在函数体中使用print语句。不是打印,而是生成字符串,然后返回它。我不知道没有他们怎么办 作业说明不使用“高级”编码,只使用我们所学的基础知识,我正在尝试使用此代码,但我的最后一个数字一直使用“无”字,而不是数字或“无”字 代码: d

我是编程新手,正在参加一个关于Python的在线学校课程,作业是从0数到一个数字(x),但跳过0,数字之间没有空格或线条,还用单词“西红柿”替换偶数

正在寻找类似以下内容的答案:
01tomato3tomato5tomato7tomato9tomato

说明中说不要在函数体中使用print语句。不是打印,而是生成字符串,然后返回它。我不知道没有他们怎么办

作业说明不使用“高级”编码,只使用我们所学的基础知识,我正在尝试使用此代码,但我的最后一个数字一直使用“无”字,而不是数字或“无”字

代码:

def soup(x):
    for i in range(x):
        if i>0 and i%2==0:
            print ('tomato',end='')
        else:
            print (i,end='')
我已经搜索了几个小时,似乎找不到基本的解决办法或一个好的答案。谢谢你的帮助。

这个

def soup(x):
    for i in range(x):
        if i>0 and i%2==0:
            print 'tomato' #,end=''
        else:
            print i #,end=''
给这个

0
1
tomato
3
tomato
5
tomato
7
tomato
9
当你执行这个

soup(10)

在Python2.7中,使变量
foo
等于
,并连接而不是打印,然后返回

def foo(n):
  # we are going build string instead of printing
  # start with 0 as it will be our base case.
  s = '0'
  # loop to n
  for i in range(n):
    # skip 0
    if i > 0:
      # if even, add to s, tomato
      if i % 2 == 0:
        s += 'tomato'
      else:
      # else, add i, but cast it to string
        s += str(i)
  return s

print(foo(10))

def foo_cleanup(n):
  # we are going build string instead of printing
  # start with 0 as it will be our base case.
  s = '0'
  # loop to n, skipping 0 all together
  # might look like a small clean up, but consider n > 1 billion
  # that is 1 billion redundant checks
  for i in range(1,n):
    # if even, add to s, tomato
    if i % 2 == 0:
      s += 'tomato'
    else:
    # else, add i, but cast it to string
      s += str(i)
  return s

# list comprehension, not always best, but useful
def foo_advanced(n):
  s = '0'+''.join(['tomato' if i % 2 ==0 else str(i) for i in range(1,n)])
  return s
print(foo_advanced(10))
def soup(x):
    foo = ""
    for i in range(x):
        if i>0 and i%2==0:
            foo += "tomato"
        else:
            foo += str(i)
    return foo

你如何调用你的代码?这似乎正是你所要求的。也许你的意思是有一个一个接一个的错误?如果要打印从零开始的所有数字,包括
x
,则需要对范围(x+1)中的i执行
,因为范围的结束参数是独占的。另外,您是否会这样调用代码:
print(soup(10))
?因为这就是为什么您会得到
None
。没有返回值的函数隐式返回
None
。你只需要这样称呼它:
soup(10)
。另外,要跳过零,只需对范围(1,x+1)中的i使用
,而不是检查
i>0
是否作为
soup(10)
print(soup(10))
运行?您是在pythonshell中运行它,还是通常在pythonscript.py中运行它?它希望我在返回中包含0。如果我需要到10,它会想要:01Tomato3Tomato5Tomato7Tomato9Tomato9None,但我得到:01Tomato3Tomato5Tomato5Tomato7Tomato9None,如果我将范围更改为n+1,它给了我:01tomato3tomato5tomato7tomato9tomatoNone@VISQL这是为了防止每个返回的数字或单词之间出现空格或行号。我注释了
end=''
之类的东西,因为它对我没有任何用处。实际上我需要它们全部放在一行,它们之间没有空格,就像这样:01tomato3tomato5tomato7tomato9tomato谢谢,另一条评论对我的Python 3.5有所帮助。他没有将整数作为字符串返回,而是作为整数返回。因此,该解决方案不具有相同或预期的输出。不过很接近。也许可以用列表来代替。@VISQL对不起,我不清楚你的说法,谁是“他”?billyeast根本没有返回语句。billyeast还表示,目标是建立一个“字符串,然后返回它”。为什么列表是更正确的返回?他没有将整数作为字符串返回,而是作为整数返回。因此,该解决方案不具有相同或预期的输出。不过很接近。也许用列表代替。不知道你在说什么。当你打印1时,1是字符串吗?@taesu这太完美了,谢谢你的回复和解释评论。它解决了我的问题,我学到了一些新东西。@billyeast283不客气。确保你完全理解答案,这就是我花时间写这些评论的原因!中亚
''.join(str(x) if x == 0 or x % 2 else 'tomato' for x in range(0, 10))