vendredi 25 mai 2018

Python - Elif not working, and it is just skipped

This is my code for a ANPR camera, which scans the license, plate and also check if they were speeding or not and writes all the information to a text document named output.txt. All of it works other than one part, see the bottom. Also no error code comes up for it

#Imports
import time
import random

#Gets the number plate
np=input()

#Sets the parts of the license plate
twoletters=np[0:2]
twonumbers=np[2:4]
threeletters=np[4:7]

#Sensors
sensor1=0
sensor2=random.uniform(0.25, 0.4)
sensor2=round(sensor2, 1)

#Variables
twoletterstf=False
twonumberstf=False
threeletterstf=False
valid=False
speed=0
ticketprice=0
ticket=False
distance=10

#Calculations to work out the speed
time.sleep(1)
distance=10
times=sensor2-sensor1
ans=1/times
speed=distance*ans



#If more than 7 letters and numbers
if len(np)>7 or len(np)<7:
    valid=False
    print("Invalid Nuber Plate")

#If less than 7 letters and numbers outputs number plate and speed

#If their is upper case letters and digits in correct order
elif twoletters.isupper() and twoletters.isalpha and twonumbers.isdigit() 
and threeletters.isupper() and threeletters.isalpha():
    valid=True
    twoletterstf=True
    twonumberstf=True
    threeletterstf=True


#If isn't upper case or their isn't 2 numbers or if their isnt't three letters, then outputs the number plate with speed
elif twoletterstf==False or twonumberstf==False or threeletterstf==False:
    print("Invalid Number Plate")
    valid=False

elif speed > 31:
    ticket=True
    ticketprice=100+speed

elif speed < 31:
    ticket=False
    ticketprice=0

else:
    exit



f=open("Output.txt", "a")
f.write("Number Plate:" + str(np) + "\n")
f.write("Valid:" + str(valid) + "\n")
f.write("Speed:" + str(speed) + "M/S" + "\n")
f.write("Ticket:" + str(ticket) + "\n")
f.write("Ticket Price: £" + str(ticketprice) + "\n")
f.write("\n")
f.close()

This is the problem, the elifs aren't actually working, and so it acts as if they weren't there. And when it writers the code in the text document it always comes up as false and never true if they are actually speeding So if anyone could help it would be a real help

elif speed > 31:
    ticket=True
    ticketprice=100+speed

elif speed < 31:
    ticket=False
    ticketprice=0

Aucun commentaire:

Enregistrer un commentaire