Download presentation
Presentation is loading. Please wait.
1
GCSE Computing
2
Random Numbers Dice game – generate two random numbers and add them together. Import random – required as this module contains code that allows the random number generator to work, saves us time to add extra code. random.randint(1,6) random no 1-6 random.randrange(6) random no 1-6
3
Dice add game import random die1 = random.randint(1,6)
die2 = random.randrange(6)+1 total = die1 + die2 print(“total scored :”, total) input(“\n\nPress enter key to continue”) We will return to develop this code later.
4
While loop y Again variable again =“y” while again != “n”: line1 line2 line3 etc. again = input(“Would you like to repeat? [y/n]”) This loop will repeat lines 1-3 until the user enters n. The criteria for the loop could change e.g. while total !=7:
5
While Loop (Condition controlled Loop)
Write a mini program that prints the following two lines and then loops to repeat if the user selects y. This is my first independent use of the While loop It’s not really that difficult!!! Would you like to view again? [y/n] Once you are happy that this works include a While Loop into your dice game program to allow the user to play again.
6
Times Tables Using While Loop
Using the While Loop run a program that prints out the times table for a number of your choice up to 12. For example the 5 times table should display like this: ….. etc. 60 end
7
Times Tables Using While Loop
n = 0 while n < 65: print (n) n += 5 How could you use this code to find out if a particular number is a multiple of your chosen multiplication table? E.G. does 3 go into 972
8
For loop (Count Controlled Loop)
for i in range (0,5): print(“Looping”) #this will repeat the print code 5 times. Create a program that outputs the word computing 15 times.
9
For loop (Count Controlled Loop)
for i in range (0,5): print(i, “Looping”) #this will include the variable i inside the loop Create a program that asks for a number then outputs it’s times table. E.g. 1 times 7 is times 7 is 14…etc.
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.