5
0/2

An incomplete Python program for a raffle ticket lottery is shown in Figure 1 below.

The program should generate a random winning ticket number between 50 and 150 (including 50 and 150). This will be the secret ticket number the user has to guess.

Figure 1

1      import random
2
3      print("Enter your guess for the winning ticket")
4      userGuess = int(input())
5      while userGuess < 50 or userGuess > 150:
6          print("Invalid ticket number")
7          userGuess = int(input())
8      print("Valid guess entered")
9      if winningTicket == userGuess:
10         print("Congratulations, you won!")

Write the Python code that should be used on line 2 in Figure 1 to:

  • generate a random integer between 50 and 150 inclusive
  • assign this number to the appropriate variable from the program.

You must use random.randrange(a, b) in your Python code.

Note that random.randrange(a, b) generates a random integer in the range a a\,a to bbb, starting at a a\,a but finishing one before bbb (exclusive of bbb).

[2]

Random number generation in a programming language Questions

Practise AQA GCSE Computer Science Random number generation in a programming language with exam-style questions for GCSE Computer Science. 8 questions, matched to the AQA GCSE Computer Science (8525) specification and written in Paper 1 and Paper 2 style. Every question includes a full worked solution and mark scheme, so you can see where marks are awarded rather than just whether you got the answer right.

PreviousNext

Random number generation in a programming language Questions

  1. GCSE
  2. /Computer Science
  3. /Random number generation in a programming language