Python.
Roll a dice.
#life decisions
import random
decisions=["Stay in Bed","Go for a walk","Go clothes shopping","Hang out with friends", "Do some programming"]
print("What to do today:", random.choice(decisions))
#roll a dice
import random
print("Roll a six to win")
number=random.randrange(1,7)
print("Roll is",number)
Guess the roll of the dice
#____________________
def ask_user(prompt):
ug=0
while not(1<=ug<=6):
try:
ug=int(input(prompt))
if not(1<=ug<=6):
raise ValueError()
except ValueError:
prompt="Invalid entry. It must be 1 to 6."
return ug
#___________________________
spin_count=0
wins=0
while True:
users_guess = ask_user("Guess the roll of the dice")
number=random.randrange(1,7)
spin_count=spin_count+1
print("Number of spins:{}. The roll is:{}".format(spin_count, number))
if number==users_guess:
wins+=1
win_percent=wins/spin_count
print("This is a win number{}!. You win percentage is {:.2f}".format(wins,win_percent))
else:
print("You lose")
Reading a file
#Read my Timetable
#-----------------
def readfile(day):
filename=day+".txt"
channel=open(filename,"r+")
lesson=channel.readlines()
channel.close()
return(lesson)
#-------------------
day=input("Day of Week")
lesson=readfile(day)
print(lesson)
print("Day- ",day,"\n")
for i in lesson:
print(i)
Teaching Ideas.
Agbonline Teaching Exercises.
Using python.com
20 code Challenges( complete for GCSE 9-1 )
Python Exercises.
Introduction to Python
Python exercises