Week 4


///3. While loops,

write a script that uses a while-loop so that it counts forward from 1 to 10 in threes, i.e. 1, 4, 7,10. 

i=1

while i <=10 :

    print(i)

    i += 3

///4.Functions

Create a function that converts C to F:

def ConvertCtoF(Celsius):

Fahrenheit = Celsius * 1.8 + 32

return Celsius # Return the conversion value

///5. More functions

Create a while-loop that runs indefinitely and each time it executes it asks the user to input a figure in C to be converted to F degrees, then whenever the user inputs a particular value, your function should be called to perform the conversion from C to F degrees.

def ConvertCtoF(Celsius):

Fahrenheit = Celsius * 1.8 + 32

return Celsius # Return the conversion value  

     while (1==1):

         print('Enter your number:')

         x = input()   

ConvertCtoF(x) #calls the function to calculate the Celcius to Fahrenheit

///7. Multiple functions

a)the sum of the marks for all modules in year 1 in a function called sum 

def Sum (module1, module2, module3, module4, module5, module6):

return Sum (module1 + module2 + module3 + module4 + module5 + module6)


b) 25% of the overall mark achieved in modules two and three

def Units_Two_And_Three(module2, module3):

return Units_Two_And_Three((module2 +module3)*(25/100))

c) the average overall mark achieved across all four modules in year 1 

def Average_Overall(module1,module2,module3,module4):

return Average_Overall((module1 + module2 + module3 + module4)/4)

d)the weighted average overall mark achieved across all modules in year 1, assuming that units one, two and three are worth 20% of the final year mark each, while unit four is worth 40% of the final mark

def Weighted_Average(module1,module2,module3,module4,module5,module6):

return Weighted_Average((module1 + module2 + module3)*(20/100) + (module4 + module5 + module6)*(40/100))


///9. Function checklist

With functions you can make your own pieces of code and incorporate them to your main script. You can also name pieces of code with the same way that variables name values such as strings and numbers. To create a function we begin by using the word def. We can not name a function 

#this one is like scripts with argv

def print_two(*args): #we know exactly how many arguements we want so this is pointless

    arg1, arg2 = args

    print("arg1: %r, arg2: %r" % (arg1, arg2))

    

def print_two_again(arg1, arg2): #this is another way to create the same function

    print("arg1: %r, arg2: %r" % (arg1, arg2))

def print_one(arg1): # this function only takes one argument

    print ("arg1: %r" % arg1)

def print_none(): # this function takes no arguments

    print ("I got nothin'.")


Questions about functions:

1. Did you start your function definition with def?

2. Does your function name have only characters and _ (underscore) characters?

3. Did you put an open parenthesis ( right after the function name)?

4. Did you put your arguments after the parenthesis ( separated by commas)?

5. Did you make each argument unique (meaning there was no duplicated names)?

6. Did you put a close parenthesis ) and a colon : after the arguments?

7. Did you indent by four spaces all lines of code you want in the function? No more, no less?

8. Did you “end” your function by going back to writing with no indent?


Before calling a function check:

1. Did you call/use/run this function by typing its name?

2. Did you put the (-character after the name to run it?

3. Did you put the values you want into the parentheses separated by commas? 4. Did you end the function call with a )-character?

Leave a comment

Log in with itch.io to leave a comment.