小编Lia*_*lle的帖子

需要帮助在python中计算一组数字的平均值

在我的数据文件中

60,66,88,90,44,90,80,77

所有数字都在一行中

这是我的代码,它没有给我平均数字

    inFile3 = open("data2.txt","r") 
    global gradeVar 
    gradeVar = len(gradeArray) 
    global Total 
    Total = 0 
    for Numbers in inFile3: 
            Total = Total + int(Numbers) 
    inFile3.close() 
    global averageVar 
    averageVar = Total/gradeVar 
    return averageVar 
Run Code Online (Sandbox Code Playgroud)

这是错误

Traceback (most recent call last):
  File "program.py", line 81, in <module>
    main()
  File "program.py", line 5, in main
    averageVar = Average() 
  File "program.py", line 39, in Average
    Total = Total + int(Numbers) 
ValueError: invalid literal for int() with base 10: '60,66,88,90,44,90,80,77\n'
Run Code Online (Sandbox Code Playgroud)

python file-io average

4
推荐指数
1
解决办法
1811
查看次数

如何从main方法自动启动程序

这是我的代码

def main():
    # This code reads in data.txt and loads it into an array
    # Array will be used to add friends, remove and list
    # when we quit, we'll overwrite original friends.txt with
    # contents 

    print"Welcome to the program"

    print "Enter the correct number"
    print "Hockey fan 1, basketball fan 2, cricket fan 3"
    choice = input("Select an option")

    while choice!=3:
        if choice==1:
            addString = raw_input("Who is your favorite player??")
            print "I love Kessel"
        elif choice==2:
            remInt = …
Run Code Online (Sandbox Code Playgroud)

python

0
推荐指数
1
解决办法
1万
查看次数

我在第42行(第4行最后一行)收到语法错误,我无法修复它可以有人请帮助吗?

def main():
    # This code reads in data.txt and loads it into an array
    # Array will be used to add friends, remove and list
    # when we quit, we'll overwrite original friends.txt with
    # contents 

    print"Welcome to the program"

    print "Enter the correct number"
    print "Hockey fan 1, basketball fan 2, cricket fan 3,Numbers of favorite players-4"
    choice = input("Select an option")

    while choice!=3:
        if choice==1:
            addString = raw_input("Who is your favorite player??")
            print "I love Kessel"
        elif choice==2: …
Run Code Online (Sandbox Code Playgroud)

python

0
推荐指数
1
解决办法
603
查看次数

标签 统计

python ×3

average ×1

file-io ×1