Gingerbread Man
Dark Magus
Thanks Taé. Will fix.
As of 5 minutes from now, the top thread will be updated.
As of 5 minutes from now, the top thread will be updated.
Blackbird_SR-71 said:when will the next lesson be just wondering?
[i]function_name[/i]([i]parameters[/i])
a = multiply(70)
a = 350
# this line makes 'a' equal whatever you type in
a = raw_input("Type in something, and it will be repeated on screen:")
# this line prints what 'a' is now worth
print a
a = "hello"
print "hello"
START PROGRAM
print opening message
while we let the program run, do this:
#Print what options you have
print Option 1 - add
print Option 2 - subtract
print Option 3 - multiply
print Option 4 - divide
print Option 5 - quit program
ask for which option is is you want
if it is option 1:
ask for first number
ask for second number
add them together
print the result onscreen
if it is option 2:
ask for first number
ask for second number
subtract one from the other
print the result onscreen
if it is option 3:
ask for first number
ask for second number
multiply!
print the result onscreen
if it is option 4:
ask for first number
ask for second number
divide one by the other
print the result onscreen
if it is option 5:
tell the loop to stop looping
Print onscreen a goodbye message
END PROGRAM
#calculator program
#this variable tells the loop whether it should loop or not.
# 1 means loop. anything else means don't loop.
loop = 1
#this variable holds the user's choice in the menu:
choice = 0
while loop == 1:
#print what options you have
print "Welcome to calculator.py"
print "your options are:"
print " "
print "1) Addition"
print "2) Subtraction"
print "3) Multiplication"
print "4) Division"
print "5) Quit calculator.py"
print " "
choice = input("Choose your option: ")
if choice == 1:
add1 = input("Add this: ")
add2 = input("to this: ")
print add1, "+", add2, "=", add1 + add2
elif choice == 2:
sub2 = input("Subtract this: ")
sub1 = input("from this: ")
print sub1, "-", sub2, "=", sub1 - sub2
elif choice == 3:
mul1 = input("Multiply this: ")
mul2 = input("with this: ")
print mul1, "*", mul2, "=", mul1 * mul2
elif choice == 4:
div1 = input("Divide this: ")
div2 = input("by this: ")
print div1, "/", div2, "=", div1 / div2
elif choice == 5:
loop = 0
print "Thankyou for using calculator.py!"
def [i]function_name[/i]([i]parameter_1_variable[/i],[i]parameter_2_variable[/i]):
{this is the code in the function}
{more code}
{more code}
return {text, or number to return to the main program}
{this code isn't in the function}
{because it isn't indented}
#remember to put a colon ":" at the end of the line that starts with 'def'
So in this sense, functions aren't really cutting and pasting code in a convenient way. It is more like a miniture program that some parameters are given to - it then runs itself, and then returns a value. Your main program sees only the returned value. If that function flew to the moon and back, and then at the end hada variable is just a stored value. To the computer, the variable 'a' doesn't look like 'a' - it looks like the value that is stored inside it. Functions are similar - to the computer, they look like the value of what they give in return of running.
return 1
#Below is the function
def hello():
print hello
return 1234
#And here is the function being used
print hello()
[color=blue]hello
1234[/color]
#calculator program
#NO CODE IS REALLY RUN HERE, IT IS ONLY TELLING US WHAT WE WILL DO LATER
#Here we will define our functions
#this prints the main menu, and prompts for a choice
def menu():
#print what options you have
print "Welcome to calculator.py"
print "your options are:"
print " "
print "1) Addition"
print "2) Subtraction"
print "3) Multiplication"
print "4) Division"
print "5) Quit calculator.py"
print " "
return input ("Choose your option: ")
#this adds two numbers given
def add(a,b):
print a, "+", b, "=", a + b
#this subtracts two numbers given
def sub(a,b):
print b, "-", a, "=", b - a
#this multiplies two numbers given
def mul(a,b):
print a, "*", b, "=", a * b
#this divides two numbers given
def div(a,b):
print a, "/", b, "=", a / b
#NOW THE PROGRAM REALLY STARTS, AS CODE IS RUN
loop = 1
choice = 0
while loop == 1:
choice = menu()
if choice == 1:
add(input("Add this: "),input("to this: "))
elif choice == 2:
sub(input("Subtract this: "),input("from this: "))
elif choice == 3:
mul(input("Multiply this: "),input("by this: "))
elif choice == 4:
div(input("Divide this: "),input("by this: "))
elif choice == 5:
loop = 0
print "Thankyou for using calculator.py!"
#NOW THE PROGRAM REALLY FINISHES
add(2,30)
num1 = 45
num2 = 7
add(num1,num2)
add(45,7)
I'm already doing something along those lines. I've been drawing information from a beginner's guide to python, and re-organising the sections in more complete lessons.croxis said:done. gbm have you thought of just taking what is used in th epython documentation and just doing minor edits ot make it understabable?
months = ('January','February','March','April','May','June',\
'July','August','September','October','November','December')
Index Value
0 January
1 February
2 March
3 April
4 May
5 June
6 July
7 August
8 September
9 October
10 November
11 December
#Telling the computer the names of the months:
months = ('January','February','March','April','May','June',\
'July','August','September','October','November','December')
#printing January onscreen:
print months[0]
#printing October:
print months[9]
#create a variable, and put 'August' in it
x = months[7]
print x
#put the months of (southern hemisphere) winter in another variable:
winter = months[5:8]
print winter
cats = ['Tom', 'Snappy', 'Kitty', 'Jessie', 'Chester']
print cats[2]
cats.append('Catherine')
#add a new value to the end of a list:
[i]list_name[/i].append([u]value-to-add[/u])
#e.g. to add the number 5038 to the list 'numbers':
numbers.append(5038)
#Remove your 2nd cat, Snappy. Woe is you.
del cats[1]
#Make the phone book:
phonebook = {'Andrew Parson':8806336, 'Emily Everett':6784346, 'Peter Power':7658344, 'Lewis Lame':1122345}
#Print out Lewis Lame's number:
print phonebook['Lewis Lame']
#Add the person 'Gingerbread Man' to the phonebook:
phonebook['Gingerbread Man'] = 1234567
#(Didn't think I would give you my real number now, would I?)
del phonebook['Andrew Parson']
#A few examples of a dictionary
#First we define the dictionary
#it will have nothing in it this time
ages = {}
#Add a couple of names to the dictionary
ages['Sue'] = 23
ages['Peter'] = 19
ages['Andrew'] = 78
ages['Karren'] = 45
#Use the function has_key() -
#This function takes this form:
#[i]function_name[/i].has_key([u]key-name[/u])
#It returns TRUE if the dictionary has [u]key-name[/u] in it
#but returns FALSE if it doesn't
#remember - this is how 'if' statements work - they run if something is true
#and they don't when something is false.
if ages.has_key('Sue'):
print "Sue is in the dictionary. She is", ages['Sue'], "years old"
else:
print "Sue is not in the dictionary"
#Use the function keys() -
#This function returns a list of all the names of the keys.
#E.g.
print "The following people are in the dictionary:"
print ages.keys()
#You could use this function to put all the key names in a list:
keys = ages.keys()
#You can also get a list of all the values in a dictionary.
#You use the values() function:
print "People are aged the following:", ages.values()
#Put it in a list:
values = ages.values()
#You can sort lists, with the sort() function
#It will sort all values in a list alphabetically, numerically, etc...
#You can't sort dictionaries - they are in no particular order
print keys
keys.sort()
print keys
print values
values.sort()
print values
#You can find the number of entries with the len() function:
print "The dictionary has", len(ages), "entries in it"