# Doing Ex. 2 # 2.a and checking my answer a,b=5,3 print a print b # 2.b and checking my answer c='test' print a print b print c # 2.c and checking my answer a=a%b print a print b print c # 2.d, trying print for more variables at once andcallable() checking my answer b*=b print a, b, c print b print c # 2.e and checking my answer a=c[1] print a, b, c # 2.f and checking my answer c=a=b=True print a, b, c # 2.g and checking my answer c=not(a or b) print a, b, c # 2.h and checking my answer a=b or c print a, b, c # 2.i and checking my answer b=str(a) + "Love" print a, b, c #Exercise 3 # creating the 3 variables according to slide #breakingDistance = (V/10)*(V/10) #reactionDistance = (V/10)*3 #stoppingDistance = reactionDistance + breakingDistance # trying to run stoppingDistance like a function as on slide 11 # stoppingDistance(80) Failed # stoppingDistance(V=80) Failed # googled making functions: http://docs.python.org/release/1.5.1p1/tut/functions.html & and using exercise3.py def car_stopping_dist(v): breakingDistance = (v/10)*(v/10) reactionDistance = (v/10)*3 stoppingDistance = reactionDistance + breakingDistance print stoppingDistance car_stopping_dist(80.0) #Yay! car_stopping_dist(50.0) car_stopping_dist(100.0) # Exercise 4 using exercise4.py import math print math.pi def cone_volume(r, h): SurfaceArea= math.pi * r * h + math.pi * r * r Volume= 1.0/3.0 * math.pi * r * r * h print SurfaceArea print Volume # checking function cone_volume(2.0,5.0) cone_volume(2.7,12.5) # Exercise 5 using exercise5.py def celsius_to_fahrenheit(C): F= C * 1.8 + 32 print F print C # checking funchtion without input variable celsius_to_fahrenheit(-10) # checking function with input variable input = -10 celsius_to_fahrenheit(input) # Exercise 6 using exercise6.py def indian_takeaway(menu_number): x=int(menu_number) - 1 menu = ['Pizza','Sushi','Rice','Stamppot','Shoarma','Chocolate Cake'] print menu[x] # checking function indian_takeaway(2) # Exercise 7 # just running to see what happens #x = (5==6) #y = str(x) #print y + [1] # printing in between to check wat really happens x = (5==6) print x y = str(x) print y ''' print y + 1 gives an error because you can not put an integer and a string together, print y + [1] gives an error because you can not put an list and a string together''' print y + 'kaas'
Run
Reset
Share
Import
Link
Embed
Language▼
English
中文
Python Fiddle
Python Cloud IDE
Follow @python_fiddle
Browser Version Not Supported
Due to Python Fiddle's reliance on advanced JavaScript techniques, older browsers might have problems running it correctly. Please download the latest version of your favourite browser.
Chrome 10+
Firefox 4+
Safari 5+
IE 10+
Let me try anyway!
url:
Go
Python Snippet
Stackoverflow Question