Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Python

practicing python, getting undesired values appended to a list, don't understand why

import random
import os
import sys

car_list = ["350z", "porche", "tiburon", "mini cooper"]
color_list = ["black", "teal", "silver", "orange", "white"]
times_list = []

def race():
    mph = random.randint(90, 120)
    time = 3600/mph
    time = round(time, 1)
    print("Your top speed was {} mph".format(mph))
    print("Your time for the race is {} seconds".format(time))
    times_list.append(time)
    for times in times_list:
        print(times)# <--- here I see an extra time...
        # and I dont see where it is coming from
        # first time through one time is appended
        # next loop two times are appended
        # and each loop after that
        # two times are appended
        # each time is a different value
        # so it doesn't appear that the times
        # are just being printed twice






while True:
    os.system('cls||clear')
    car = random.choice(car_list)
    car_color = random.choice(color_list)
    print("Your car is a {} {}.".format(car_color, car))

    print("The race is a one mile stretch.")
    ready = input("Are you ready to race? Y/n ").lower()
    if ready == "y":
        race()
        race_again = input("Race again? Y/n ").lower()
        if race_again == "y":
            race()
        else:
            sys.exit()

    else:
        sys.exit()

1 Answer

Ryan S
Ryan S
27,276 Points

Hi John,

The problem is that when you call race() the second time in your while loop, the screen clears before you can see the results. What is happening is that the 2nd function call runs like it should, (your times_list is still getting appended with the second race time), but there is nothing to pause the display before it returns to the beginning of the loop and clears the screen. So for every one run of the while loop, you will be generating 2 race times.

If you look at the logic of the main points in your loop:

1) Clear Screen

2) prompt user input -> (ready?)

3) run race()

4) prompt user input -> (race_again? ) Results from previous race are displayed because of prompt pausing the loop.

5) run race() and immediately return to step one.

If you were to insert an input prompt right after the second race(), you can use it to pause the loop and see the second race time.

Hope this makes sense.

Makes perfect sense. Thank you so much Ryan. I appreciate you taking the time to take a look.