140 likes | 290 Vues
COP 2510. Chapter 6 - Functions. Random function ( randint ). #import the desired function import random #integer random number in the range of 1 through 100. #(The values 1 and 100 are included in the range.) number = random.randint (1, 100)
E N D
COP 2510 Chapter 6 - Functions
Random function (randint) #import the desired function import random #integer random number in the range of 1 through 100. #(The values 1 and 100 are included in the range.) number = random.randint(1, 100) #note that the call to the random.randint function appears #on the right side of an = operator print number
Random function (randint) # This program displays five random # numbers in the range of 1 through 100. import random def main(): for count in range(5): # Get a random number. number = random.randint(1, 100) # Display the number. print number # Call the main function. main()
Random function (randrange) #assigns a random number in the range of 0 through 9 #to the number variable import random number = random.randrange(10) print number
Defining our own functions #Program 6‐2 (total_ages.py) # This program uses the return value of a function. def main(): # Get the user's age. first_age = input('Enter your age: ') # Get the user's best friend's age. second_age = input("Enter your best friend's age: ") # Get the sum of both ages. total = sum(first_age, second_age) # Display the total age. print 'Together you are', total, 'years old.' # The sum function accepts two numeric arguments and # returns the sum of those arguments. def sum(num1, num2): result = num1 + num2 return result # Call the main function. main()
Defining our own functions defis_even(number): # Determine whether number is even. If it is, # set status to true. Otherwise, set statusto false if (number % 2) == 0: status = True else: status = False # Return the value of the status variable. return status value = input('Enter a number: ') if is_even(value): print 'The number is even.' else: print 'The number is odd.'
Math functions import math result = math.sqrt(16) print result radius = input("Enter radius: ") area = math.pi * radius**2 print area
Formatting Numbers myValue = 7.23456 print 'The value is %.2f' % myValue print 'The value is %.3f' % myValue print 'The value is %.1f' % myValue • When the operand on the left side of the % symbol is a string, it becomes the string format operator • The f that the format specifier indicates that we want to display a floating‐point number. • The .2 that appears before the f indicates that the number should be rounded to two decimal places.
Formatting Multiple values value1 = 6.7891234 value2 = 1.2345678 print 'The values are %.1f and %.3f' % (value1, value2) • print string % (number, number, …) • In the print statement, the %.1f format specifier corresponds to the value1 variable and the %.3f format specifier corresponds to the value2 variable. • When the code runs it will produce the following output: The values are 6.8 and 1.235
Formatting Integers dogs = 2 cats = 3 print 'We have %d dogs and %d cats.' % (dogs, cats)
String Functions #Getting a String's length #Use the len function to get the length of a string name = 'Charlemagne' strlen = len(name) print strlen #Appending Strings msg1='This is ' msg2 = 'one string.' message = msg1 + msg2 print message
String Functions #The upper and lower Methods littleName = 'herman' bigName = littleName.upper() print bigName bigName= 'HERMAN' littleName = bigName.lower() print littleName
String Functions #The find method string = 'Four score and seven years ago' position = string.find('seven') if position != -1: print 'The word "seven" was found at index', position else: print 'The word "seven" was not found.' This code will display: The word "seven" was found at index 15 (if we remove all the text between the quotes in the string variables, the code would display: The word "seven" was not found)
String Functions #Slicing full_name = 'Patty Lynn Smith' middle_name = full_name[6:10] print middle_name #How would you slice the last name? last_name = full_name[11:] print last_name #and the first name? first_name = full_name[:5] print first_name