如何处理初学者的编程任务?

时间:2013-02-01 07:31:48

标签: python string for-loop

我是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 个答案:

答案 0 :(得分: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循环所做的。

答案 1 :(得分: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(),这是进行字符串格式化的新方法 - 它非常漂亮:)

答案 2 :(得分:0)

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

有许多与Python相关的资源,包括官方文档,可以帮助您使用str()等特定功能。

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

答案 3 :(得分:-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