Python Forum

Full Version: Error in code
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
So below my code i have got everything working. However when there is an amount of errors in entering race times for each lane. It adds an extra lane. so if input 4 racers, and within entering the times you put wrong info in. it will end up asking for 5 lanes. Can anyone see why?

counter=0                                    
specTime=0                                   
laneNum=[]
gender = 0
genderList = ["m" ,"M", "f" , "F"]
numRacers= 0

while gender not in genderList:
    gender=input("Please enter whether it is a male or female race. M/F ")
worldrecords=[9.58,9.86,9.87]
while gender == "M" or gender == "m":
    try:
        while (numRacers < 4) or (numRacers > 8):
            numRacers= int(input("Please input how many racers will be participating? There's a limit from 4-8 racers. "))
            if (numRacers < 4) or (numRacers > 8):
                print("Error")
        for count in range(numRacers):
            specTime=counter+1
            time=float(input("Please enter the lap time for lane " +str(specTime)+  " to 2 decimal places. "))
            num=round(time, 2)               # turns the time into two decimal places if the user has put more than two DP
            laneNum.append(num)
            if laneNum[counter]< worldrecords[0]:
                print("Congratulations! World record, European record and British record has been achieved!")
            elif laneNum[counter]< worldrecords[1]:
                print("Congratulations! European record and British Record has been achieved!")
            elif laneNum[counter]< worldrecords[2]:
                print("Congratulations! British record has been achieved!") # the above if statements will notify the user if a record has been achieved when the time is inputted
            counter=counter+1
            laneNum.sort() #This will order the inputed race times from fastest to slowest. 
            print(laneNum)
    except:
        print("Error")
        continue # the above validation will only allow the user to input numbers 4-8, anything else entered will prompt the user to re enter a number
    break
worldrecords2=[10.49,10.73,10.99]
while gender == "F" or gender == "f":
    try:
        while (numRacers < 4) or (numRacers > 8):
            numRacers= int(input("Please input how many racers will be participating? There's a limit from 4-8 racers. "))
            if (numRacers < 4) or (numRacers > 8):
                print("Error")
        for count in range(numRacers):
            specTime=counter+1
            time=float(input("Please enter the lap time for lane " +str(specTime)+  " to 2 decimal places. "))
            num=round(time, 2)               # turns the time into two decimal places if the user has put more than two DP
            laneNum.append(num)
            if laneNum[counter]< worldrecords2[0]:
                print("Congratulations! World record, European record and British record has been achieved!")
            elif laneNum[counter]< worldrecords2[1]:
                print("Congratulations! European record and British Record has been achieved!")
            elif laneNum[counter]< worldrecords2 [2]:
                print("Congratulations! British record has been achieved!") # the above if statements will notify the user if a record has been achieved when the time is inputted
            counter=counter+1
            laneNum.sort() #This will order the inputed race times from fastest to slowest. 
            print(laneNum)
    except:
        print("Error")
        continue # the above validation will only allow the user to input numbers 4-8, anything else entered will prompt the user to re enter a number
    break
I put wrong info in ("A") and it only asks for 4 lanes:

Output:
Please enter whether it is a male or female race. M/F M Please input how many racers will be participating? There's a limit from 4-8 racers. 4 Please enter the lap time for lane 1 to 2 decimal places. A Error Please enter the lap time for lane 1 to 2 decimal places. 1 Congratulations! World record, European record and British record has been achieved! [1.0] Please enter the lap time for lane 2 to 2 decimal places. 1 Congratulations! World record, European record and British record has been achieved! [1.0, 1.0] Please enter the lap time for lane 3 to 2 decimal places. 1 Congratulations! World record, European record and British record has been achieved! [1.0, 1.0, 1.0] Please enter the lap time for lane 4 to 2 decimal places. 1 Congratulations! World record, European record and British record has been achieved! [1.0, 1.0, 1.0, 1.0]
Any example of a sequence that makes it fail?
Please enter whether it is a male or female race. M/F m
Please input how many racers will be participating? There's a limit from 4-8 racers. 4
Please enter the lap time for lane 1 to 2 decimal places. ;
Error
Please enter the lap time for lane 1 to 2 decimal places. 10
[10.0]
Please enter the lap time for lane 2 to 2 decimal places. p
Error
Please enter the lap time for lane 2 to 2 decimal places. p
Error
Please enter the lap time for lane 2 to 2 decimal places. 12
[10.0, 12.0]
Please enter the lap time for lane 3 to 2 decimal places. 54
[10.0, 12.0, 54.0]
Please enter the lap time for lane 4 to 2 decimal places. p
Error
Please enter the lap time for lane 4 to 2 decimal places. 10
[10.0, 10.0, 12.0, 54.0]
Please enter the lap time for lane 5 to 2 decimal places. 12
[10.0, 10.0, 12.0, 12.0, 54.0]
Please enter the lap time for lane 6 to 2 decimal places. 14
[10.0, 10.0, 12.0, 12.0, 14.0, 54.0]
Please enter the lap time for lane 7 to 2 decimal places. 18
[10.0, 10.0, 12.0, 12.0, 14.0, 18.0, 54.0]



This is one i did that discovered the problem
Well, you catch the error with Try, it goes back to the beginning of the while and then when it reaches the for loop it starts from 0 again. So it will always look for 4 new records after every error.

You could change the for loop to something like:

while number_records < numRacers:
And increase number_records everytime they go ok
Because the try/except includes the for loop, but the error occurs in the for loop, it starts the for loop all over again, which gets it to ask for extra times. This is one reason why you narrow the scope of your try/except blocks. It should just be around the input statement.

Another problem here is that you duplicate all of your code for the male and female cases. This seems to only be for the checking the records. It would be better to have the world records stored in a dict or list, and then select the records to use for the loop and run one copy of the code.

Also, this code:

while condition:
    do_something()
    break
Is equivalent to:

if condition:
    do_something()
I expect you did that so you could use continue in the except block, but as I said, that's actually your problem.
I’m new to python, so the helps greatly appreciated, how would you all think the best way to re write my code would be