0% found this document useful (0 votes)
26 views

1 Intro To Python Solutions

Copyright
© © All Rights Reserved
Available Formats
Download as PDF or read online on Scribd
0% found this document useful (0 votes)
26 views

1 Intro To Python Solutions

Copyright
© © All Rights Reserved
Available Formats
Download as PDF or read online on Scribd
You are on page 1/ 4
1 Introducing Python - solutions to exercises Table of Contents > [Llneoducing Python - solutions to exercises ‘Li Peining on LLL Adding Dexibilty © 1.2 Finding the area ofa rstanele 121 Adding fexibilsy ais 14 Basie Arithmetic LI. Printing on sereen Ccreatea function named show_address that will display your name and your mailing aess, The funtion doesnot need to take any addtional info or return anything, Therefore you may just call it ike -show_address() and it should print on screen something like: your name street name and suburb postcode and city country “The function will have empty parameter list and will not have the return. statment. I [1] 1 def show_address(): 2 print('Jon Snow) 3 print (‘Mystery Road, IceTree") 4 print ('1235, Pentos") 5 print (*westeros") In [2]: 1. show_address() Jon Snow Mystery Road, TceTree 1235, Pentos westeros 1.1.1 Adding flexibility Modify you previous solution so that show_address accepts additional information, the ame ofthe person to print the addess. For example, when you call it show_adéress("Janes Cook" ) it will printout James Cook name followed by your adress. In [3] 1 deF show_address(name, street, suburb, postcode, city, country): 2 print(nane) 3 print(street, suburb) 4 print (postcode, city) 5 print(eountry) In [4] 1 show_address(*Jack Snell", ‘West Ave.", "Kingswood", °2231', ‘Silver City’, ‘Greenland') Jack Snell West Ave. Kingswood 3231 Silver city Greenland In [5]: 1 show_address(*Andy Shark’, "South Ave.', "Woodside’, 1231", "Iced City’, "Redland" ) Andy Share South ave, Woodside 1231 Iced city Redland 1.2 Finding the area of a rectangle Write function #ind_area that asks the user to enter the wid and length ofa rectangular room, Once these values have been read, your program should compute and display the area ofthe room, The length and the width will be entered as leating-point numbers. Include units n your prompt and output message. Calling the function by name Find_area() should produce the message like: ‘The area of your room is 24.4 square metres. Ofcourse, the actual number wil differ for your toom, In [6] 1 def Find area() # Read the width and height of the room floor width = float(input(‘Enter width of the room: *)) height = Float(input("Enter height of the room: ")) ropply formula (known from school) for area of a reactangle area = width * height print out/display the output print('The area of the room is ¥.1f square metres.” Xarea) In 7}: 1. #ind_area() Enter width of the roon: 3 Enter height of the room: 4.2 ‘The area of the room is 12.6 square metres. In [8]: 1 det Find_area(nane) 2 width = float(input(*Enter width of the room: *)) 3 height = Float(input ("Enter height of the room: ")) 4 area = width * height 5 print("The area of your room Xs is %.1# square metres.’ ¥(name,area)) In [3] 4 find_area(‘Julie') Enter width of the reon: 4.6 Enter height of the room: 2.4 ‘The area of your room Julie is 11.0 square eetres. 1.2.1 Adding flexi ity “Modify you previous solution to find she volume ofa room and not asking the user for input. Instead, the requited input willbe passed to the function through arguments. Cal the function Find_volume . It will accept four pieces of additional infrmation: person's name, loor_width, floor height, and room_ height For exarmple, when you call it find_volume(*Julie', 5, 3.21, 2.7) iwill printout The voluse of your room Julie is 43.36 cubic metres. [Note that you donot ned to eal the required information using. Input. function, In [10] 1 def Find_voluse(nane, floor_width, floor_height, room_height): 2 valune = floor_width * floor. height*roon_hesgnt 3 print (‘The volume of your roan 5 is ¥.1f cubic metres.’ X(nane, volune)) I [11] A #ind_volume(Julie', 5, 3.21, 2.7) ‘The volune of your roon Julie is 43.3 cubic retres. 1.3 Free fall Given the height from which the objec is dropped in metes, write a function named free_fal1_speed that determines how quickly an objet is traveling when ithits the ground, Assume the objec’ inital speed is ey and the acceleration due to gravity is constant a = 9.8m (as you iat intl reall fom shoo), You can use the formula v = fof + 2ad to compat th inal velocity, when he intl speed yy and distance, dre known. You may either ask the user for input of 1» and dor pass this info tothe function when you call ike free_fall_speed(1@, 20) For practice you may as well provide both implementations, Printout the answer with 3 decimal laces, Ta calculate the square rot use the sport statement io enable sant function from math import sqrt Im [12] def free_f011_speed(ini_speed, distance) from math import sqrt # befine a constant for the acceleration due to gravity in m/s**2 GRAVITY = 9.8 # Compute the final velocity vf = sqrt(ini_speed**2 + 2 * GRAVITY * distance) # Display the result print("Tt will hit the ground at ¥.2f m/s." % vf) a 2 3 4 5 6 7 8 ° 2 2 ay [13] 1. free_fal1_speed(19,20) It will hit the ground at 22.18 m/s. allematvely,you could ask for user for al (or some) input values In [34] 1 def Free_fall_speed2(ini_speed): 2 from math inport sqrt a 4 # Define a constant for the acceleration due to gravity in m/s**2 5 GRaviTy = 9.8 7 Read the height from which the object is dropped B distance = Floatinput("Enter height (in seters): ")) 9 10 Compute the final velocity 11 vf = sqrt(ini_speed**2 + 2 * GRAVITY * distance) 2 13 # Display the result 14 print(*rt will hit the ground at %.2F m/s." X vf) 1s 1n [25] 1. free_fall_speed2(10) Enter height (in meters): 12.7 Tt will hit the ground at 18.68 m/s. 1.4 Ba Create a funetion that reads two integers, x and y, ftom the usr. It should compute and display the sum x + y, difference x — y, product x « y, quotient xy and the power 7 Arithmetic In [36] 1 def arithnetic(): 2" # Read the input values from the user 3 x= tntCinpus("Enter the value of x: ")) 4 y= ineCinpus(’Enter the value of y: ")) 5 # Compute and display the sun, difference, product, quotient and power © print(x, “="s ys ‘iss x #y) 7 print Qe *="5 ys Tis" x = ¥) 8 print, 5 ys 38s x ty) 9 print, “3 ys 38) x Ly) 18 printOxs "1, ys "is', xy) an [37] 2 arsthmetie() Enter the value of x: 5 Enter the value of y: 7 S+7 isa Tis -2 7 is 35 7 is 0.7102857142857143, 7 As 78125

You might also like