2016-02-12 15 views
1

我需要帮助将我的汽车注册号牌写在文件中(只有那些打破了限速的汽车) 。我的代码部分有效,但我在登记牌上遇到了麻烦......它只打印了一个打破速度限制的许多限制。另外,我需要它在不同的路线。这是我的代码(谢谢你提前):汽车登记号码正在以错误的方式打印到文件中(不是我想要的方式)

varlimit = 70 
import time 
import re 
def speedcam(): 
criminallist = [] 

print (' ') 
regpat = "[A-Z][A-Z][0-9][0-9]\s[A-Z][A-Z][A-Z]" 
reg = input('Insert your registration number: ') 

while re.match(regpat, reg): 
    print ('Your registration number is: ',reg) 
    time.sleep(2) 
    print (' ') 
    time_taken = float(input('Enter time taken: ')) 
    print ('The time taken is: {:.2f} minutes'.format(time_taken)) 
    distance = float(input('Enter distance: ')) 
    print ('The distance is: {:.2f} ' .format(distance) ,'miles') 
    speed_limit = distance/(time_taken/60) 
    time.sleep(2) 
    print(' ') 
    print ('The speed limit is: {:.2f}' .format(speed_limit) , 'mph') 
    time.sleep(1) 

    if speed_limit > varlimit: 
     print ('Your registration number, "',reg , '" has been forwarded to the police"') 
     criminallist.append(reg) 

    file = open("speeders.txt","w") 
    file.write = (criminallist+'\n') 
    file.close() 

    if speed_limit < varlimit: 
     print ('You drive well. thank you for following the rules.') 
    time.sleep(2) 
    print (' ') 

    question = input('Do you want to continue? (Y/N) :') 
    if question == 'Y': 
     print (' ') 
     speedcam() 
    if question == 'N': 
     print ('--Program Terminated--') 
     print (' ') 
     print (' ') 
     print ('Now printing list of cars breaking the speed limt.') 
     print (' ') 
     print (' LIST OF CARS BREAKING THE SPEED LIMIT OR WITH INVALID REGISTRATION NUMBERS:') 

    print (criminallist) 



else: 
    print ('Your registration number, ',reg , 'is invalid, and it has been forwarded to the police') 
    criminallist.append(reg) 

    question = input('Do you want to continue? (Y/N) :') 
    if question == 'Y': 
     print (' ') 
     speedcam() 
    if question == 'N': 
     print ('--Program Terminated--') 
     print (' ') 
     print (' ') 
     print ('Now printing list of cars breaking the speed limt.') 
     print (' ') 
     print (' LIST OF CARS BREAKING THE SPEED LIMIT OR WITH INVALID REGISTRATION NUMBERS:') 

    print (criminallist) 

    file = open("speeders.txt","w") 
    file.write = (criminallist+'\n') 
    file.close() 

回答

1

您正在使用w当您打开了书面文件,但w截断该文件,然后重新开始。相反,您可能想要追加a

+0

它现在有效!非常感谢你!! –