这是我的代码:
import re
import time
uk=open("uknp.txt", "w")
nnstd=open("nnstdnp.txt", "w")
uk.close()
nnstd.close()
while 1:
distance=1
print("------------------------------------")
registration = input("Please Enter the Registration Plate: ").lower()
time = float(input("How long did it take you to reach 1 mile in seconds: "))
speed=((distance/time)*60)*60
print("Car",registration,"was going at" ,"%.2f" %speed,"Mph")
if speed>60:
if re.match("[a-z]{2}[0-9]{2}[a-z]{3}!", registration):
uk=open("uknp.txt", "a")
uk.write(registration + speed)
uk.close()
else:
nnstd.open("nnstd.txt", "a")
nnstd.write(registration+speed)
nnstd.close()
意味着将登记牌和速度上传到文本文件,如果它有效或无效。
答案 0 :(得分:0)
您的代码中有多处错误。
nnstdnp.txt
文件,然后写一个nnstd.txt
nnstd.open("nnstd.txt", "a")
nnstd=open("nnstd.txt", "a")
TypeError: Can't convert 'float' object to str implicitly
xx.write(registration + speed)
中的字符串和浮点数(=>引发xx.write(registration + ("%02f" % speed))
)
你没有结束条件退出while 1:
循环;它可能是:
registration = input("Please Enter the Registration Plate: ").lower()
if len(registration) == 0: break
作为一般建议,当出现问题时,您应该尝试添加print
次来看看究竟会发生什么,以及跟随if
的哪些分支。