简体   繁体   English

给定前 10 个数字的范围,从开始数字迭代到结束数字并打印当前数字和前一个数字的总和

[英]Given a range of first 10 numbers, Iterate from start number to the end number and print the sum of the current number and previous number

I'm trying to write Python code for the above problem and getting an error code.我正在尝试为上述问题编写 Python 代码并获得错误代码。 I would appreciate help:我会很感激帮助:

num = list(range(10))
previousNum = 0
for i in num:
    sum = previousNum + i
    print('Current Number '+ str(i) + 'Previous Number ' + str(previousNum) + 'is ' + str(sum)
    previousNum=i

This is the error I get:这是我得到的错误:

File "<ipython-input-40-6f1cd8f8f1d7>", line 6
    previousNum=i
              ^
SyntaxError: invalid syntax

Looks to be a simple syntax error in line 5.看起来是第 5 行中的一个简单语法错误。

You're missing a closing parenthesis ")" at the end of your print function.您在打印 function 末尾缺少右括号“)”。

For example:例如:

num = list(range(10))
previousNum = 0
for i in num:
    sum = previousNum + i
    print('Current Number '+ str(i) + 'Previous Number ' + str(previousNum) + 'is ' + str(sum)) # <- This is the issue.
    previousNum=i

Also, here are 3 pointers to improve your code that might be useful for you:此外,这里有 3 个改进您的代码的指针,它们可能对您有用:

  1. First off, Python uses snake case for it's language as described in PEP8 , so instead of typing "previousNum" you should use "previous_num", so we'll start with that.首先,Python 使用 Snake case 作为其语言,如PEP8中所述,因此您应该使用“previous_num”而不是输入“previousNum”,所以我们将从它开始。

  2. Storing list(range(1)) in this instance isn't needed.不需要在此实例中存储list(range(1)) You can just use the ```range(10)''' function in place of having a stored list of ranges.您可以只使用 ```range(10)''' function 来代替存储的范围列表。

  3. f strings are much more readable ways of doing String Concatenation (adding strings together). f 字符串是进行字符串连接(将字符串添加在一起)的更具可读性的方式。

With these your code will look like this:有了这些,您的代码将如下所示:

previous_num = 0
for i in range(10):
    sum = previous_num + i
    print(f'Current number {i} Previous Number {previous_num} is {sum}')
    previous_num = i

Happy coding!快乐编码!

sum = 0
for idx in range(10):
    print(f'current number = {idx}')
    if idx != 0:
        sum += idx
        print(f'cumul. sum = {sum}')

Welcome to Stackoverflow.欢迎来到 Stackoverflow。 There's always functional python:总有功能 python:

>>> list(map( lambda x: x[0] + x[1], zip(range(0,10), range(1,11))))
[1, 3, 5, 7, 9, 11, 13, 15, 17, 19]

Or if you meant a cumulative sum of predecessors, like a reduce:或者,如果您的意思是前辈的累积总和,例如减少:

>>> [sum(range(xs)) for xs in range(1,11)]
[0, 1, 3, 6, 10, 15, 21, 28, 36, 45]

This used to be easier in python 2, before range, filter, and map were turned into iterators.在将范围、过滤器和 map 转换为迭代器之前,这在 python 2 中曾经更容易。

for i in range(1,10+1):
   print(i+i-1, end=' ')

Here it iterates from 1 to 10 inclusively.在这里它从 1 迭代到 10 包括在内。 It prints the value of sum of current value of i and the previous value that is i-1它打印 i 的当前值与 i-1 的前一个值之和的值

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM