无法理解的循环Python [英] Can't Understand For Loops Python

查看:126
本文介绍了无法理解的循环Python的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我一直试图让我的头周围的python循环,但我不明白他们是如何工作的。我试图创建一个for循环,将通过一个数字列表,并将它们加在一起,但我真的在努力如何语法工作。



从我所理解的语法是:

pre $ for w in words:
print w,len(w)

有人可以解释一下这个迭代变量是如何工作的(w),也许使用一个数字列表的例子? / b>

我试过这个,但是我觉得我错了。

  numbers = raw_input(>)
addition = 0
数字中的数字:
addition = addition + number
打印另外


解决方案 ,将该值赋给像 w number 这样的变量,然后执行操作然后转到下一个项目,直到它们用完。



让我们先看看您的示例...

  numberstring = raw_input(> )#期望一个字符串,如1 2.0 4 100
addition = 0
numberlist = numberstring.split()#将字符转换为单词列表
in numberlist:
addition = addition + float(number)
print addition

如果不使用 .split()来转换列表,循环将遍历字符串的每个字母,将其解释为字母而不是数字。作为数字(使用 float())它甚至可以将字符串添加回去(除非在这种情况下已经初始化为零)。

列表混淆的一个来源:变量的名称并不重要,如果你在myvar:中用字母表示,它不会强制程序选择字母,它只是把下一个项目赋值给变量 letter 。在Python中,这个项目可以是一个单词,数字等的列表,或者如果给定一个字符串,它将采取字符作为项目。

另一种设想的方式是,你有一个购物篮里装满了物品,收银员一次一个地循环着它们:

 对于mybasket中的每个项目:
#将项目添加到总计
#转到下一个项目。

如果你递给他们一袋苹果,并要求他们循环,苹果,但如果你给他们一个篮子,他们会把这个袋子作为一个项目...

So I've been trying to get my head around for loops inside of python but I just can't understand how they work. I'm trying to create a for loop that will go through a list of numbers and add them all together but I'm really struggling with how the syntax works.

From what I understand the syntax is:

for w in words:
    print w, len(w)

Can someone please explain how this iterating variable works (w) and maybe using the example of a list of numbers?

I tried this but I think I have it all wrong.

numbers = raw_input("> ")
addition = 0
for number in numbers:
    addition = addition + number
print addition

解决方案

For loops take each item in a thing, assign that value to a variable like w or number, do an operation, and then move on to the next item until they run out.

Let's get your example working first...

numberstring = raw_input("> ")  # expect a string like "1 2.0 4 100"
addition = 0
numberlist = numberstring.split(" ") # convert the chars into a list of "words"
for number in numberlist:
    addition = addition + float(number)
print addition

Without coverting to a list using .split(), your loop will move through each letter of the string, interpreting it as a letter instead of a number. And without converting those words as numbers (using float()) it could even add the string back together (except in this case there has been an initialization with zero).

One source of confusion on lists: the NAME of the variable doesn't matter. If you say for letter in myvar: it doesn't force the program to choose letters. It just takes the next item and assigns that value to the variable letter. In Python this item can be a list of words, numbers, etc, or if given a string, it will take characters as the items.

Another way to envision it is that you have a shopping basket full of items, and the cashier is looping through them one at a time:

for eachitem in mybasket: 
    # add item to total
    # go to next item.

If you handed them a bag of apples and asked them to loop through it, they would take out each apple, but if you give them a basket, they will take the bag out as one item...

这篇关于无法理解的循环Python的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆