D. Tunus
D. Tunus

Reputation: 271

writing to file error with 3 arguments?

I have a task to design a code that outputs cars that have a foreign number plate and are speeding all at the same time, out of 10 different number plates, either UK or not. Part of the task is to display the excess speed of those foreign & speeding cars. I would like to find an efficient way to fit an output sentence into one line, for the excess speed bit. (I am new to "write to file")

import re

# DATA
distance = 0.06 # Distance between the Camera A and B; 0.06 = 600 metres
speed_limit = 20 # (meters per second)

number_plates = ["DV61 GGB",      #UK
                 "D3S11 EUBG 20", #F
                 "5T314 10A02",   #F
                 "24TEG 5063",    #F
                 "TR09 TRE",      #UK
                 "524 WAL 75",    #F
                 "TR44 VCZ",      #UK
                 "FR52 SWD",      #UK
                 "100 GBS 12",    #F
                 "HG55 BPO"       #UK
                 ]

enter = [7.12,7.17,7.22,7.12,7.23,7.41,7.18,7.25,7.11,7.38]
leave = [7.56,7.39,7.49,7.56,7.45,7.57,7.22,7.31,7.59,7.47]

# Find the non-UK plates
pattern = "(?![A-Z]{2}\d{2}\s+[A-Z]{3}$)"
foreign_numbers = list(filter(lambda x: re.match(pattern, x), number_plates))

# Calculations for speed
elapsed = [(l - e)/100 for l, e in zip(leave, enter)]
speed = [distance/t for t in elapsed]

# Dictionary for foreign speeders + 2 conditions
foreign_speeders = {plate: speed 
                    for plate, speed in zip(number_plates, speed)
                    if (plate in foreign_numbers) and (speed > speed_limit)}

print("10 cars have passed Camera A, then Camera B\nSpeed limit is 20 meters per second.\n")

# Write foreign 
for plate, speed in foreign_speeders.items():

    speeders_data = open("speeders.txt","w") # Opens file with name of "speeders.txt"
    speeders_data.write("{0:>14s} is foreign and is speeding at{1:5.1f} mps".format(plate, speed),", and has an excess speed of",speed-speed_limit)
    speeders_data = open("speeders.txt","r")
    print(speeders_data.read())
    speeders_data.close()

I receive this:

>>> 
10 cars have passed Camera A, then Camera B
Speed limit is 20 meters per second.

Traceback (most recent call last):
  File "M:\ICT Coursework\Task 2.1.py", line 41, in <module>
    speeders_data.write("{0:>14s} is foreign and is speeding at{1:5.1f} mps".format(plate, speed),", and has an excess speed of",speed-speed_limit)
TypeError: write() takes exactly 1 argument (3 given)
>>> 

Upvotes: 0

Views: 104

Answers (2)

albert
albert

Reputation: 8593

The syntax of the string you're writing to the file seems to be wrong. Instead of

"{0:>14s} is foreign and is speeding at{1:5.1f} mps".format(plate, speed),", and has an excess speed of",speed-speed_limit)

Try something like this since file.write() accepts one string only:

"{0:>14s} is foreign and is speeding at{1:5.1f} mps and has an excess speed of {2:5.1f} mps".format(plate, speed, speed-speed_limit)

Upvotes: 2

Kenly
Kenly

Reputation: 26708

In this line speeders_data.write("{0:>14s} is foreign and is speeding at{1:5.1f} mps".format(plate, speed),", and has an excess speed of",speed-speed_limit) you call write function with 3 arguments,
There is two ,.
Pass one string

speeders_data.write("{0:>14s} is foreign and is speeding at{1:5.1f} mps , and has an excess speed of {2:5.1f}".format(plate, speed, speed-speed_limit))

Upvotes: 2

Related Questions