Skip to content

Course home

Sign up

Develop code

EasyMediumHard
1234567891011121314151617
Question 12

A program is written for a music playlist creator. It displays the names of available songs, stored in an array. The user enters a number. That number of songs is selected randomly from the array and displayed.

Original Code (Q02.py)

impor random

songList = ["Starlight", "Yesterday", Blackbird,
            "Respect", "Heroes"]

print("Available songs:")
for song in songList
    print("- " + song)

numSongs = int(input("How many songs would you like to play? "))

print("
Your random playlist:")
for count in range(numSongs - 1):
    index = random.
    name = songList[index + 1]
    print(naime)

Amend the code to:

  • fix the syntax error on line 1 (impor random)
  • fix the syntax error on line 8 (for song in songList)
  • complete line 16 to generate a random integer between 0 and 4 (index = random.)
  • complete line 3 to make all the song names be string data types (songList = ...)
  • fix the IndexError on line 17 (name = songList[index + 1])
  • fix the NameError on line 18 (print(naime))
  • fix the logic error on line 15 that causes one less song to be printed than is asked for (for count in range(numSongs - 1):)
  • use white space to improve the readability of the code.

Do not change the basic functionality of the correct parts of the code. Do not add any additional features.

[8]
Markscheme

Develop code Questions

  1. GCSE
  2. /Computer Science
  3. /Develop code

51 exam-style questions on Edexcel GCSE Computer Science Develop code, covering Decomposition and abstraction to solve problems, Read, write, analyse and refine programs, Converting algorithms into programs, Techniques for readable, maintainable code, Identifying and correcting program errors, and Evaluating program fitness and efficiency. Each one has a worked solution and a mark scheme showing where the marks go.

Question bank