• Homework
  • Thirds
  • Upper Thirds
  • Fourth
  • Divisions
    • ICT
    • Computer Science
  • Fifth
  • Lower Sixth
  • Upper Sixth
    • Upper Sixth Theory
  • Blog
  • Careers
  • Unity
  • Networking

Random numbers in Python

To create random numbers in python there are two steps.
  • Step 1:  add the line 'import random' at the very beginning of your code.
  • Step 2: use the command 'random.randrange(maximumnumber)'
For example:
import random
while True:
    print(random.randrange(100))


Submit your answer here

Picture
This program will print random numbers between 0 and 99 on the screen over and over again.

Number guessing challenge

Make the program 'think of a number'
Create a variable called myNumber and assign a random number between 0 and 99
Create a variable called correctGuess and assign its value as false
Create a variable called myGuess and assign its value as 0
Loop for as long as correctGuess is false
    prompt the user to enter a number
    use int(raw_input()) to read the number into the variable myGuess
    if myGuess is equal to myNumber, then the user has won.  Say "Well Done" and set correctGuess to true
    if myGuess is less than myNumber, then say "Too Low"
    if myGuess is greater than myNumber, then say "Too High"
What is the maximum number of guesses that you should need to be able to find any number between 0 and 99?

Can you make the program keep a score - if the player goes above the maximum number, the computer should declare victory and show the number it was thinking of.
Powered by Create your own unique website with customizable templates.