我目前在CS125课程的项目中遇到问题。该项目的前提是编写一个程序,询问用户他们有多少南瓜重量,然后读取许多南瓜重量,打印每个重量与评论重,正常或光。然后程序必须对权重进行平均并将它们显示为三位小数。该程序的重点是使用多个功能而不仅仅是一个主要功能,这是我开始遇到麻烦的地方。我相信我的程序刚刚完成,但我在接近结束时收到了名称错误。这是迄今为止的代码:
clickMe= wait(driver, 10).until(EC.element_to_be_clickable((By.XPATH,'(//div[div/div/text()="Main Lists"]//div[starts-with(@class, "sm-CouponLink_Label") and normalize-space()])[%s]' % str(index + 1))))
clickMe.click()
当我运行程序时,它可以正常工作,直到达到calcAverage,我得到以下错误:
# File: weights.py
# Date: 10/25/2017
# Author: Marcus Kassab
# Purpose: Print the average weight of a given number of pumpkins
def intro():
print()
print("Program to calculate the average of a")
print("group of pumpkin weights.")
print("You will be asked to enter the number of")
print("pumpkins, followed by each pumpkin weight.")
print("Written by Marcus Kassab.")
print()
def getPumpkins():
numPumpkins = int(input("Enter the number of pumpkins: "))
print() #for turnin
#print()
totalWeight = 0
count = 0
for pumpkins in range(numPumpkins):
count = count + 1
pumpkinWeight = int(input("Enter the weight for pumpkin " + str(count) +": "))
#print() #for turnin
totalWeight = totalWeight + pumpkinWeight
if pumpkinWeight >= 70:
print("{0:0.3f}".format(pumpkinWeight), "is heavy.")
elif pumpkinWeight >= 50 and pumpkinWeight < 70:
print("{0:0.3f}".format(pumpkinWeight), "is normal.")
else:
print("{0:0.3f}".format(pumpkinWeight), "is light.")
return numPumpkins, totalWeight
def calcAverage(totalWeight, numPumpkins):
avg = float(totalWeight) / float(numPumpkins)
print("The average weight of the", numPumpkins, "pumpkins is, "{0:0.3f}".format(avg))
def main():
intro()
getPumpkins()
calcAverage(totalWeight, numPumpkins)
main()
我不确定错误在哪里,因为我认为在getPumpkins()中定义totalWeight会起作用。但是,在calcAverage中的某个地方似乎无法正常工作。任何有关此错误的帮助将不胜感激!
答案 0 :(得分:0)
您要从getPumpkins()
中的main()
返回变量,但在尝试在calcAverage(totalWeight, numPumpkins)
的下一行使用它们之前,您不会将它们存储在任何位置。
答案 1 :(得分:0)
getPumpkins()
正在将一个元组返回main
并且您没有将其分配给任何内容。更改main()
numPumpkins, totalWeight = getPumpkins()