Why can't i type a float as an integer in my f-string

I had a class that was already connected to a csv file so i couldnt add ‘marathon’ there.

The query was that
a club wants to display the number of whole marathons each member has walked. A marathon is 26.22 miles long.

An example of the calculation required is:

If Nikolai Bryant walks 145.6 miles this equates to:
145.6/26.22 miles = 5.59115179 marathons
Nikolai has therefore walked 5 whole marathons.

In the above example ‘Nikolai,Bryant,5’ would be stored.

Write “The number of whole marathons walked by each member is” to the results.txt file

Start loop for each record in members() array

Calculate the number of whole marathons walked

Write the forename, surname and the number of whole marathons to the results.txt file and loop

Close the results.txt file

I had this which tried to make marathon a new variable since adding it to the class not in this code would’ve caused an error

def output_it(members, furthest):
  of=open("results.txt", "a")
  of.write('The number of whole marathons walked is:\n')
  for loop in range(len(members)):
    int(marathon=round(1, members[loop].distance/26.22))
    of=open("results.txt", "a")
    of.write(f'{members[loop].forename} {members[loop].surname} {marathon}')
#Meinne programme
display_fursthest_distance_walked(furthest, members)
output_it(members, furthest)

>Solution :

This line doesn’t seem valid to me. marathon is not an argument of int(); this code is not creating a variable called marathon as far as I know. int() returns an int, but you’re not even doing anything with the returned value. You’re just throwing it away:

int(marathon=round(1, members[loop].distance/26.22))

It seems that you meant to do:

marathon = int(round(1, members[loop].distance / 26.22))

Also, having magic numbers in your code is an anti-pattern. Instead, I would define the number somewhere above, like:

# A marathon is 42.195 kilometers, or 26 miles, 385 yards:
MILES_PER_MARATHON = 26.0 + (385 * 3 / 5280)

Then, your code would make more sense to an uninformed reader:

marathon = int(round(1, members[loop].distance / MILES_PER_MARATHON))

Leave a Reply