Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Class Task
#1
Hey, i need help with a class task I got today.

A program is required to read the data for each member from the text file. The program should use this data to find then display the furthest distance walked. The names of every member who has walked more than 70% of the furthest distance should be written to an empty text file so that the file can be printed out later.

The text file is :

Nikolai,Bryant,145.6
Susan,Brown,34.2
Teressa,Jones,398.5
Martin,Daly,256.9
Ross,Durrant,409.0
Greg,Watson,99.2
Wendy,Russell,87.4
Pamela,Adkins,73.6
Ian,Hunter,385.7
James,Kerr,505.2
Lesley,Wallace,68.4
Kim,Pettigrew,256.4
Steven,Johnstone,23.4
Ali,Hussain,12.1
Hasan,Abbas,302.0
Jacek,Nowak,199.9
Mirka,Kowalski,176.8
Rudo,Hyper,120.2
Tisa,Sullivan,484.2
Albert,Nvodo,385.8

So far i tried saving the Name,Surname and Distance as seperate lists(arrays) and later on i tried using the max() function to find the furthest distance walked, but the anwser i always get is 99.2 when it actually is 505.2, I think it is because they are stored as strings insted of floats and it somehow messes it up. Sorry for sounding so unprofessional but i just started learning python 2 weeks ago.
Reply
#2
You can use the built-in float() to convert a string to a floating point number.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#3
import csv
with open('members.txt', 'r') as f:
    Name = [row[0] for row in csv.reader(f, delimiter=',')]
    print(Name)

with open('members.txt', 'r') as f1:
    Surname = [row[1] for row in csv.reader(f1, delimiter=',')]
    print(Surname)

with open('members.txt', 'r') as f2:
    Distance = [row[2] for row in csv.reader(f2, delimiter=',')]
    print(Distance)

print(float(max(Distance)))
Output:
['Nikolai', 'Susan', 'Teressa', 'Martin', 'Ross', 'Greg', 'Wendy', 'Pamela', 'Ian', 'James', 'Lesley', 'Kim', 'Steven', 'Ali', 'Hasan', 'Jacek', 'Mirka', 'Rudo', 'Tisa', 'Albert'] ['Bryant', 'Brown', 'Jones', 'Daly', 'Durrant', 'Watson', 'Russell', 'Adkins', 'Hunter', 'Kerr', 'Wallace', 'Pettigrew', 'Johnstone', 'Hussain', 'Abbas', 'Nowak', 'Kowalski', 'Hyper', 'Sullivan', 'Nvodo'] ['145.6', '34.2', '398.5', '256.9', '409.0', '99.2', '87.4', '73.6', '385.7', '505.2', '68.4', '256.4', '23.4', '12.1', '302.0', '199.9', '176.8', '120.2', '484.2', '385.8'] 99.2
This is what i did so far. Yeah i know its a mess, and probably very inefficient... Any help is welcome :))
Reply
#4
You need to do float on the individual numbers (line 11) before you apply max.

You can do it all with one opening of the file. Note that zip() can be used to transpose a list of lists.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply


Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020