guiromero Postado Janeiro 14, 2023 Compartilhar Postado Janeiro 14, 2023 Olá, Estou fazendo um exercício cujo enunciado é o seguinte: " Write a program that prompts for a file name, then opens that file and reads through the file, looking for lines of the form: X-DSPAM-Confidence: 0.8475 Count these lines and extract the floating point values from each of the lines and compute the average of those values and produce an output as shown below. Do not use the sum() function or a variable named sum in your solution. You can download the sample data at http://www.py4e.com/code3/mbox-short.txt when you are testing below enter mbox-short.txt as the file name." A resposta deve ser: “Average spam confidence: 0.7507185185185187” O código que criei está abaixo: ................... fname = input("Enter file name: ") fh = open(fname) i=0 tot = 0 for line in fh: if not line.startswith("X-DSPAM-Confidence:"): continue v = (line[20:26]) value = float(v) i=i+1 tot = tot + value av = (float(tot/i)) print(av) ......................... A resposta que aparece é uma lista de valores médios acumulados. Quero extrair só o último valor (0.7507185185185187), como faço? Obrigado. Link to comment Compartilhe em outros sites Outras opções de compartilhamento...
Postagens Recomendadas
Crie uma conta ou entre para comentar 😀
Você precisa ser um membro para deixar um comentário.
Crie a sua conta
Participe da nossa comunidade, crie sua conta.
Criar minha conta agoraÉ bem rápido!
Entrar
Você já tem uma conta?
Entrar agoraFaça o login agora.