#Function Definition
def next_fun():
print "Inside next_fun()"
def third_fun():
print "Inside third_fun()"
def main():
print "First function called main()"
#Function Call
next_fun()
third_fun()
print "main() is completed"
#Function Call
main()
#Function Calls
def name_print():
print "C++ is Fun!\tC++ is Fun!\tC++ is Fun!"
print " C++ i s F u n ! \t C++ i s F u n ! \t C++ i s F u n ! "
reverse_print()
def reverse_print():
print "!nuF si ++C\t!nuF si ++C\t!nuF si ++C"
def one_per_line():
print "C++\n i\n s\n F\n u\n n\n !"
def main():
for ctr in range(1,6,1):
name_print() #Calls function five times.
one_per_line() #Calls the program's last function once
main()
def do_fun():
global sales #global variable
global profit #global variable
sales = 20000.00
profit=5000.00
print "The sales in the second function are: ",sales
print "The profit in the second function is: ",profit
third_fun() #Call third function to show that globals are visible
def third_fun():
print "\nIn the third function:"
print "The sales in the third function are",sales
print "The profit in the third function is ",profit
def main():
print "No variable defined in main()\n"
do_fun()
main()
def pr_again():
j=5 #Local to only pr_again().
print j,",",z,",",i
global i
i=0
def main():
p=9.0 #Local to main() only
print i,",",p
pr_again() #Calls next function.
global z
z=9.0
main()
def main():
age=input("What is your age? ") #Variable age is local to main()
get_age()
print "main()'s age is still",age
def get_age():
age=input("What is your age again? ") #A different age. This one is local to get_age().
main()
def main():
for ctr in range(0,11,1): #Loop counter
print "main()'s ctr is ",ctr,"\n"
do_fun() #Call second function
def do_fun():
for ctr in range(10,0,-1):
print "do_fun()'s ctr is ",ctr,"\n"
main()
def main():
i=10
i=20
print i," ",i,"\n"
i=30
print i," ",i," ",i,"\n"
i=10
print i," ",i," ",i
main()
def pr_init(initial):
print "Your initial is ",initial
def pr_other(age,salary):
print "You look young for",age,"and ",'%.2f' %salary,"is a lot of money"
initial=raw_input("What is your initial?")
age=input("What is your age?")
salary=input("What is your salary?")
pr_init(initial) #call pr_init() and pass it initial
pr_other(age,salary) #call pr_other and pass age and salary
def compute_sale(gallons):
#local variable
price_per=12.45
x=price_per*float(gallons) #type casting gallons because it was integer
print "The total is ",'%.2f' %x
def main():
print "Richard's Paint Service"
gallons=input("How many gallons of paint did you buy?")
compute_sale(gallons) #Function Call
main()
def main():
for ctr in range(1,26,1):
triple_it(ctr)
def triple_it(ctr):
total=0
ans=ctr*3
total+=ans
print "The number ",ctr,"multiplied by 3 is ",ans
if total>300:
print "The total of triple numbers is over 300"
main()
def moon(weight):
weight/=6
print "You weigh only ",weight,"pounds on the moon !"
def main():
weight=input("How many pounds do you weigh? ")
moon(weight) #call the moon() function and pass weight
main()
def main():
Igrade=raw_input("What letter grade do you want?")
average=input("What is your current test average")
tests=input("How many tests do you have left?")
check_grade(Igrade,average,tests) #// Calls function and passes three variables by value
def check_grade(Igrade,average,tests):
if tests==0:
print "You will get your current grade of ",Igrade
else:
if tests==1:
print "You still have time to bring up your average of",'%.1f' %average,"up . Study hard !"
else :
print "Relax. You still have plenty of time."
main()
def calc_av(num1,num2,num3):
local_avg=(num1+num2+num3) / 3 #Holds the average for these numbers
return local_avg
print "please type three numbers (such as 23 54 85) "
num1=input()
num2=input()
num3=input()
avg=calc_av(num1,num2,num3) #call function and pass the numbers
print "\n\nThe average is ",avg #Print the return value
def doub(number):
d_num=number*2 #Doubles the number.
return d_num #Returns the result.
number=input("What number do you want doubled? ")
d_number= doub(number) #Assigns return value.
print number," doubled is ",d_number
def sums(num):
sumd=0
if num<=0:
sumd=num
else:
for ctr in range(1,num+1,1):
sumd=sumd+ctr
return sumd
num=input("Please type a number: ")
sumd= sums(num)
print "The sum of the digits is " , sumd
def maximum(num1,num2):
if num1>num2:
maxi=num1
else:
maxi=num2
return maxi
def minimum(num1,num2):
if num1>num2:
mini=num2
else:
mini=num1
return mini
print "Please type two numbers ( such as 46 75 ) "
num1 = input()
num2 = input()
maxi=maximum(num1,num2) #Assign the return value of each function to variables
mini=minimum(num1,num2)
print "The minimum number is ",mini
print "The maximum number is ", maxi
tax_rate=0.07 #Assume seven percent tax rate
total_sale=input("What is the sale amount? ")
total_sale+=tax_rate*total_sale
print "The total sale is ",'%.2f' %total_sale
def ascii(num):
asc_char=chr(num) #Type cast to a character
return asc_char
num=input("Enter an ASCII number? ")
asc_char=ascii(num) #Number is passed to the function ascii()
print "The ASCII character for ",num,"is ",asc_char
def netpayfun(hours,rate,taxrate):
gross_pay=hours*rate
taxes=taxrate*gross_pay
net_pay=gross_pay-taxes
return net_pay
net_pay=netpayfun(40.0,3.50,0.20)
print "The pay for 40 hours at $3.50/hr., and a 20% tax rate is $ ",net_pay
net_pay=netpayfun(50.0,10.00,0.30)
print "The pay for 40 hours at $10.00/hr., and a 30% tax rate is $ ",net_pay
net_pay=netpayfun(10.0,5.00,0.10)
print "The pay for 40 hours at $5.00/hr., and a 10% tax rate is $ ",net_pay
i=-15
x=-64.53
ians=abs(i) #abs() function is a built in function that returns a positive value
print "Integer absolute value of -15 is ",ians
fans=abs(x)
print "Float absolute value of -64.53 is ",'%.2f' %fans
#function definition
def output(x):
if isinstance(x,int):
print x
else:
if isinstance(x,float):
print '%.2f' %x
else:
print x
#Variable Decleration
name="C++ By Example makes C++ easy!"
Ivalue=2543
fvalue=39.4321
#calling function
output(name)
output(Ivalue)
output(fvalue)