
sum = 0 for x in range(101): sum = sum + x print(sum)
报错说 invalid syntax 如果将 print 缩进, 则是将每次和都返回, 不是我想要的结果。 所以请问怎么只返回 5050
1 xiaobai987 2017-07-25 15:20:19 +08:00 |
2 xiaobai987 2017-07-25 15:20:39 +08:00 我这边可以哦 |
3 helloSwift 2017-07-25 15:43:39 +08:00 缩进的区别  |
4 rocksolid 2017-07-25 15:48:14 +08:00 print(sum([x for x in range(101)])) |
5 SuT2i 2017-07-25 15:49:37 +08:00 我这里也可以 |
8 vmwarenat8 2017-07-25 16:02:45 +08:00 @sagaxu 膜拜 6 楼 |
9 Kung815 OP @xiaobai987 新加了图片 |
10 Kung815 OP @helloSwift 第二种情况会报错, 请看新加的图片 |
11 lcdtyph 2017-07-25 23:28:52 +08:00 @Kung815 你在交互式界面输入循环判断之类的 block, 最后要留个空行, 让解释器知道 for block 结束了. 然后再输入后面的 print 语句. |
12 weyou 2017-07-25 23:31:58 +08:00 via Android 高斯说 print(5050) |
14 yangzzcc 2017-07-26 01:07:22 +08:00 via Android @helloSwift 这是什么字体? |
16 congeec 2017-07-26 05:22:03 +08:00 |
17 Xs0ul 2017-07-26 06:55:18 +08:00 先运行前两行(循环和加)外加一个空行,然后再 print 另外 number 是什么鬼( |
18 SingeeKing PRO 在解释器里面运行的话循环结束之后需要先打一个 enter,然后再输入循环外语句 |
20 ikeeper 2017-07-26 08:20:14 +08:00 @helloSwift 用的 atom 吗,请教怎么配置 python3 |
22 araraloren 2017-07-26 08:56:46 +08:00 。。。唉 垃圾 python 用个 REPL 也要注意空格。。 |
23 wizardoz 2017-07-26 09:28:51 +08:00 from functools import reduce sum = reduce(lambda a,b: a + b, range(100)) |
24 zhangsen1992 2017-07-26 09:46:36 +08:00 sum(range(1,101)) |
25 jsoncai 2017-07-26 09:48:50 +08:00 因为没有用游标卡尺 |
26 helloSwift 2017-07-26 09:53:37 +08:00 @Kung815 11 楼说的对,留空行 @yangzzcc Atom 编辑器, 主题 one dark, atom 自带的,终端我用这个 https://github.com/nathanbuchar/atom-one-dark-terminal 字体 Source Code Pro-Light 13 , https://github.com/adobe-fonts/source-code-pro @ikeeper 是 atom 直接写的,没啥特别配置 |
27 jerryshao 2017-07-26 21:21:46 +08:00 你新加的那个图... `for x in number(range(101))` 里面这 number 是哪来的? |