简体   繁体   English

列表 python 的总和 + 从列表中删除字符串

[英]Sum of a list python + removing strings from the list

I have a problem where I need to take user input ie (Jack,10,10,9,10,10) with Jack being student name and the numbers are test scores.我有一个问题,我需要接受用户输入,即 (Jack,10,10,9,10,10),Jack 是学生姓名,数字是考试成绩。 I need to find the average of those test scores and print them with the student name.我需要找到这些考试成绩的平均值并用学生姓名打印出来。 This problem seems very straightforward put I get an output error that says:这个问题看起来很简单,我得到一个 output 错误,上面写着:

>>> calcMarks()
Enter marks:Jack,10,10,9,10,10
Traceback (most recent call last):
File "<pyshell#32>", line 1, in <module>
calcMarks()
File "xyz", line 12, in calcMarks
avg = sum(list[0:len(list)])
TypeError: unsupported operand type(s) for +: 'int' and 'str'
>>> 

Here is my code so far:到目前为止,这是我的代码:

def calcMarks():
    #input = Jack,10,10,9,10,10
    userInput = input('Enter marks:')
    list = userInput.split(',')
    name = list.pop(0)
    #print(type(list))
    #print(type(name))
    avg = sum(list)/(len(list)-1)
    print(name + ' ' + avg)

avg is a number. avg是一个数字。 In order to be concatenated with other strings, it needs to be turned into a string first with str()为了和其他字符串串联,需要先用str()变成字符串

Also, you are summing strings, which need to be turned into numbers before being summed.此外,您正在对字符串求和,在求和之前需要将其转换为数字。

def calcMarks():
    #input = Jack,10,10,9,10,10
    userInput = input('Enter marks:')
    l = userInput.split(',')
    name = l.pop(0)
    l = [int(x) for x in l]
    avg = sum(l)/len(l)
    print(name + ' ' + str(avg))

The problem with your approach is that when reading from input you'll always get a string, so you'll have to cast to integer the marks to compute the mean.您的方法的问题是,从input中读取时,您总是会得到一个字符串,因此您必须将标记转换为 integer 以计算平均值。 Here's how I would do this:这是我将如何做到这一点:

def calcMarks():
    # unpack the input into user and marks
    # having split the string by ','
    user, *marks = input('Enter marks:').split(',')
    # take the average of the marks cast to int 
    avg_mark = sum(map(int,marks))/(len(marks))
    # you can use f-strings to print the output
    print(f'{user} has an average of {avg_mark}')
    # print('{} has an average of {}'.format(user, avg_mark)) # for python 3.5<

calcMarks()

Enter marks:Jack,10,10,9,10,10
Jack has an average of 9.8
def calcMarks(): #input = Jack,10,10,9,10,10 userInput = input('Enter marks:') l = userInput.split(',')[1:] #print(type(list)) #print(type(name)) return sum(map(int, l))/(len(l))

String concetanation only works when variable needs to be concatenate are having same data type .字符串concetanation仅在需要连接的变量具有same data type时才有效。

print(name + ' ' + str(avg))

If you wish to have the output of a function that returns an int or float value, you can add , instead of + in the print1 :如果您希望 function 的 output 返回intfloat值,您可以在print1中添加,而不是+

def calcMarks():
    #input = Jack,10,10,9,10,10
    userInput = input('Enter marks:')
    inputs = userInput.split(',')
    name = inputs.pop(0)
    input_list = [int(i) for i in inputs]
    #print(type(list))
    #print(type(name))
    avg = sum(input_list)/(len(input_list))
    print(name + ' ',avg)
calcMarks()

Output: Output:

Enter marks:Jack,10,10,9,10,10
Jack  9.8

You could take the username and the marks in two variables, one a simple string and other a string list.您可以将用户名和标记放在两个变量中,一个是简单字符串,另一个是字符串列表。 Then You could proceed for the average as you already separated the name from the numbers.然后您可以继续进行平均,因为您已经将名称与数字分开了。 If I'm not wrong, then this should do your work well.如果我没有错,那么这应该可以很好地完成您的工作。

def calcMarks():
    # input Jack, <marks>...
    name, marks = input("Enter Marks"), map(int, input().split(", "))
    average = sum(marks) / len(marks)
    print(name + ' ' + str(average))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM