Aug-29-2019, 03:05 PM
Hello everyone,
I'm new to coding and currently self-taught using online books. I am creating a dictionary with 30 aliens all the same, then modifying the first 3 to change colour/speed/point value.
The book I'm following gives the below example:
{'colour': 'green', 'points': 5, 'speed': 'slow'}
{'colour': 'green', 'points': 5, 'speed': 'slow'}
{'colour': 'green', 'points': 5, 'speed': 'slow'}
{'colour': 'green', 'points': 5, 'speed': 'slow'}
{'colour': 'green', 'points': 5, 'speed': 'slow'}
...
but the output in the book shows this:
{'speed': 'medium', 'colour': 'yellow', 'points': 10}
{'speed': 'medium', 'colour': 'yellow', 'points': 10}
{'speed': 'medium', 'colour': 'yellow', 'points': 10}
{'speed': 'slow', 'colour': 'green', 'points': 5}
{'speed': 'slow', 'colour': 'green', 'points': 5}
...
Is the book incorrect? and if so, how do I adapt the code to give me the right output?
I'm new to coding and currently self-taught using online books. I am creating a dictionary with 30 aliens all the same, then modifying the first 3 to change colour/speed/point value.
The book I'm following gives the below example:
# make an empty list for storing aliens aliens = [] # make 30 green aliens for alien_number in range(30): new_alien = {'colour': 'green', 'points': 5, 'speed': 'slow'} aliens.append(new_alien) # modify the first 3 aliens for alien in aliens[:3]: if alien['colour'] == 'green': alien['colour'] == 'yellow' alien['speed'] == 'medium' alien['points'] == 10 elif alien['colour'] == 'yellow': alien['colour'] == 'red' alien['speed'] == 'fast' alien['points'] == 15 # show the first 5 aliens for alien in aliens[:5]: print(alien) print("...") # show how many aliens have been created print(f"Total number of aliens: {len(aliens)}")The book gives an output different to the one I am receiving and I don't know why. The output I get is:
{'colour': 'green', 'points': 5, 'speed': 'slow'}
{'colour': 'green', 'points': 5, 'speed': 'slow'}
{'colour': 'green', 'points': 5, 'speed': 'slow'}
{'colour': 'green', 'points': 5, 'speed': 'slow'}
{'colour': 'green', 'points': 5, 'speed': 'slow'}
...
but the output in the book shows this:
{'speed': 'medium', 'colour': 'yellow', 'points': 10}
{'speed': 'medium', 'colour': 'yellow', 'points': 10}
{'speed': 'medium', 'colour': 'yellow', 'points': 10}
{'speed': 'slow', 'colour': 'green', 'points': 5}
{'speed': 'slow', 'colour': 'green', 'points': 5}
...
Is the book incorrect? and if so, how do I adapt the code to give me the right output?