2

我对 python 很陌生,正在努力为一个类创建一个程序。我们应该记录一周内每天的降雪量,并将输入四舍五入到小数点后一位,我在下面做了:

def main():
    print "~*~*~*~*~*~  SNOWFALL LOG ~*~*~*~*~*~"

    snowFall = float (raw_input("Enter Snowfall for Day 1: "))
    print "Snowfall for Day 1: ", ("%.1f" % snowFall)
    snowFall = float (raw_input("Enter Snowfall for Day 2: "))
    print "Snowfall for Day 2: ", ("%.1f" % snowFall)
    snowFall = float (raw_input("Enter Snowfall for Day 3: "))
    print "Snowfall for Day 3: ", ("%.1f" % snowFall)
    snowFall = float (raw_input("Enter Snowfall for Day 4: "))
    print "Snowfall for Day 4: ", ("%.1f" % snowFall)
    snowFall = float (raw_input("Enter Snowfall for Day 5: "))
    print "Snowfall for Day 5: ", ("%.1f" % snowFall)
    snowFall = float (raw_input("Enter Snowfall for Day 6: "))
    print "Snowfall for Day 6: ", ("%.1f" % snowFall)
    snowFall = float (raw_input("Enter Snowfall for Day 7: "))
    print "Snowfall for Day 7: ", ("%.1f" % snowFall)

main()

但是,在那之后,我们应该使用for循环计算总降雪量和平均值。我知道命令str(command)也以某种方式涉及,但我不知道如何。

我应该如何解决这个问题?

4

4 回答 4

2

我建议你做的是将值放入列表中,以存储它们。所以在你打印之前把它放进去:

snowFallTotal = []

现在您可以将值放入此列表中。每次raw_input放这个之后:

snowFallTotal.append(snowFall)

这样做是“附加”snowFall到列表中。

现在,在获得所有值之后,您可以使用for循环计算总和,然后将其除以7得到平均值。

totalSum = 0  # variable to store the sum
for i in snowFallTotal:  # iterate through the value list
    totalSum += i  # augmented addition; adds the value to the variable in place
average = totalSum / 7.0  # floating point division to get accurate average

然后您可以打印结果。

print 'Average Snowfall for the week:', ('%.1f' % average)

作为旁注,您不需要for循环。sum您可以用一个简单的函数替换它。

totalSum = sum(snowFallTotal)

这将 totalSum 分配给 中元素的总和snowFallTotal,这正是for循环所做的。

于 2013-02-01T07:39:03.333 回答
2

如果使用循环,则可以节省几行代码:

# An empty list to hold data
snowfalls = []

# Loop through each daynumber, from 1 to 7
for daynumber in range(1,8):
    snowfall = float(raw_input("Enter Snowfall for Day {}: ".format(daynumber)))
    snowfalls.append(snowfall)
    print "Snowfall for Day {}: {:.1f}".format(daynumber, snowFall)

# For debugging, print the whole list
print snowfalls

# Calculate total
total_snowfall = sum(snowfalls)
print "Total snowfall: {:.1f}".format(total_snowfall)

# Calculate average (= total / 7.0 ) 
# len() gets the length of the snowfall list
average_snowfall = total_snowfall / len (snowfalls)
print "Average snowfall: {:.1f}".format(average_snowfall)

我用过format(),这是进行字符串格式化的新方法——它非常漂亮:)

于 2013-02-01T08:50:49.217 回答
0

我建议你仔细阅读你的作业规范,并附上你的课堂笔记。很少有作业没有足够的材料让学生完成任务。

网上有很多与 Python 相关的资源,包括可以帮助您使用特定功能的官方文档,例如str().

如果您可以直接联系您的老师,请向他们提出很多问题。他们可以帮助您解决有关如何处理编程任务的任何基本问题。

于 2013-02-01T08:44:02.563 回答
-1

我可以编写一个函数来做到这一点,但我想你才刚刚开始这样做......所以也许这会有所帮助。它不是 DRY(编程的一个好规则是不要重复自己),但这种方式可能会帮助您更好地理解它!

total_snowfall=0.0

snowFall = float (raw_input("Enter Snowfall for Day 1: "))
total_snowfall +=snowFall 
print "Snowfall for Day 1: ", ("%.1f" % snowFall)

snowFall = float (raw_input("Enter Snowfall for Day 2: "))
total_snowfall +=snowFall 
print "Snowfall for Day 2: ", ("%.1f" % snowFall)

snowFall = float (raw_input("Enter Snowfall for Day 3: "))
total_snowfall +=snowFall 
print "Snowfall for Day 3: ", ("%.1f" % snowFall)

snowFall = float (raw_input("Enter Snowfall for Day 4: "))
total_snowfall +=snowFall 
print "Snowfall for Day 4: ", ("%.1f" % snowFall)

snowFall = float (raw_input("Enter Snowfall for Day 5: "))
total_snowfall +=snowFall 
print "Snowfall for Day 5: ", ("%.1f" % snowFall)

snowFall = float (raw_input("Enter Snowfall for Day 6: "))
total_snowfall +=snowFall 
print "Snowfall for Day 6: ", ("%.1f" % snowFall)

snowFall = float (raw_input("Enter Snowfall for Day 7: "))
total_snowfall +=snowFall 
print "Snowfall for Day 7: ", ("%.1f" % snowFall)

print "total snowfall is:" ,("%.1f" % total_snowfall) #you can do some calculations here
于 2013-02-01T07:48:09.783 回答