pythonwhile循环次数查询_如何使用while循环计算python代码中的条目数?(How do I count the number of entries in a python code ...

如何使用while循环计算python代码中的条目数?(How do I count the number of entries in a python code using while loops?)

我正在为我的入门python编程课做一个家庭作业,我遇到了问题。 说明是:

修改find​​_sum()函数,以便打印输入值的平均值。 与之前的average()函数不同,我们不能使用len()函数来查找序列的长度; 相反,你必须引入另一个变量来“计算”输入的值。

我不确定如何计算输入数量,如果有人能给我一个好的起点,那就太好了!

# Finds the total of a sequence of numbers entered by user

def find_sum():

total = 0

entry = raw_input("Enter a value, or q to quit: ")

while entry != "q":

total += int(entry)

entry = raw_input("Enter a value, or q to quit: ")

print "The total is", total

I'm working on a homework assignment for my introductory python programming class and I am stuck on a problem. The instructions are to:

Modify the find_sum() function so that it prints the average of the values entered. Unlike the average() function from before, we can’t use the len() function to find the length of the sequence; instead, you’ll have to introduce another variable to “count” the values as they are entered.

I am unsure on how to count the number of inputs and if anyone can give me a good starting point, that would be great!

# Finds the total of a sequence of numbers entered by user

def find_sum():

total = 0

entry = raw_input("Enter a value, or q to quit: ")

while entry != "q":

total += int(entry)

entry = raw_input("Enter a value, or q to quit: ")

print "The total is", total

原文:https://stackoverflow.com/questions/15238432

更新时间:2020-01-27 21:57

最满意答案

每次读取输入total += int(entry) ,应立即增加变量。

在其他地方将其初始化为0之后,只需要num += 1即可。

确保while循环中所有语句的缩进级别相同。 您的帖子(最初写的)没有反映任何缩进。

Every time you read an input total += int(entry), immediately afterward you should increment a variable.

num += 1 is all it would take, after you've initialized it to 0 elsewhere.

Make sure your indentation level is the same for all statements in the while loop. Your post (as originally written) did not reflect any indentation.

2013-03-06

相关问答

由于你已经有了你的距离greatcircledistance() ,我们称之为greatcircledistance() ,对于每一行都是如此: select a.id, count(b.id), avg(b.price)

from mytable a

left join mytable b

on greatcircledistance(a.latitude, a.longitude, b.latitude, b.longitude) <= 5

group by a.id;

对于40k条目,

...

好吧, COUNT 在将 HAVING应用于结果集之前工作 。 因此,如果您需要计算他们的数量 - 您必须用另一个来包装您的查询。 SELECT COUNT(*) FROM (

SELECT field.id * 10 AS foo FROM table HAVING foo > 100

)

Well, COUNT works BEFORE HAVING is applied to the result set. So if you need to count their number

...

一个更简单易懂的解决方案: l = [3, 3, -22, 1, -22, 1, 3, 0]

counter = 0

for el in l:

if l.count(el) == 1:

counter += 1

这很简单。 您迭代列表中的项目。 然后,您查看元素是否恰好在列表中一次,然后添加+1。 你可以改进代码(make liste comprehension,使用lambda表达式等等),但这是所有这一切背后的想法,也是最容易理解的,imo。 A more sim

...

尝试这个: entries.Elements("entry").Count();

Try this: entries.Elements("entry").Count();

首先,我将解释您的代码的作用,然后,代码将执行您想要的操作。 day = int(input('How many days did you work?: '))

start = 1

end = day

amount = 0.01 # Start and End shouldn't be a thing

total = 0 # I think this is what you wanted... the amount will double every time and the total will

...

每次读取输入total += int(entry) ,应立即增加变量。 在其他地方将其初始化为0之后,只需要num += 1即可。 确保while循环中所有语句的缩进级别相同。 您的帖子(最初写的)没有反映任何缩进。 Every time you read an input total += int(entry), immediately afterward you should increment a variable. num += 1 is all it would take, after

...

我相信这是你需要的吗? var startingNum = 10;

// Create a function that uses the above variable to count by twos.

function countingByTwo(num1) {

for (var i = 0; i < num1; i++) {

startingNum += 2;

console.log(startingNum);

}

return startingN

...

递归方法 如果你不介意递归(见下文),解决这个问题的一种方法是使用递归函数来迭代越来越深的维度。 例如: def iterBD(dim, breakdown):

if (dim > 0):

return [ [breakdown["name"]] + iterBD(dim - 1, sub) for sub in breakdown["breakdown"] ]

return [ [breakdown["name"]] + data["counts"] for data in br

...

你很接近,文档很轻松关于agg : df.groupby(['GROUP','GRADE']).agg({'TOTAL_SERVICE_TIME' : 'mean',

'TOTAL_WAIT_TIME' : ['mean', 'count']})

Out[45]:

TOTAL_WAIT_TIME TOTAL_SERVICE_TIME

mean c

...

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值