1. INPUT through functions - Rakhmanov
1. INPUT through functions
In [1]: C = input('C=?') C = float(C) F = (9./5)*C + 32 print (F)
C=?24 75.2
What is the difference between upper and lower program?
In [3]: C = float(input('C=?')) F = (9./5)*C + 32 print (F)
C=?24 75.2
EVAL function
In [6]: r = eval('1+2') r
Out[6]: 3
In [7]: a='1' b='2' c=eval(a+b) c
Out[7]: 12
In [8]: from math import sqrt r = eval('sqrt(2)') r
Out[8]: 1.4142135623730951
In [10]:
# Perimeter of Square def calculatePerimeter(l):
return 4*l
# Area of Square def calculateArea(l):
return l*l
expression = input("Type a function: ")
for l in range(1, 5): if (expression == 'calculatePerimeter(l)'): print("If length is ", l, ", Perimeter = ", eval(expression)) elif (expression == 'calculateArea(l)'): print("If length is ", l, ", Area = ", eval(expression)) else: print('Wrong Function') break
Type a function: calculateArea(l) If length is 1 , Area = 1 If length is 2 , Area = 4 If length is 3 , Area = 9 If length is 4 , Area = 16
EXEC
In [11]: program = 'a = 5\nb=10\nprint("Sum =", a+b)' exec(program)
Sum = 15
In [12]:
a = 5 b=10 print("Sum =", a+b)
Sum = 15
In [ ]:
program = input('Enter a program:') exec(program)
# [print(item) for item in [1, 2, 3]]
From String to code We need to install Scitools with: pip install scitools3
In [13]: from math import *
def f2(x): return exp(x)*sin(x)
In [14]: f2(2) Out[14]: 6.71884969742825
In [15]:
from scitools.StringFunction import StringFunction formula = 'exp(x)*sin(x)' f = StringFunction(formula)
# turn formula into function f(x)
In [16]: f(2) Out[16]: 6.71884969742825
2. Command line input
Cretae following programs and test through prompt
In [ ]:
# one input import sys C = float(sys.argv[1]) F = 9.0*C/5 + 32 print (F)
In [ ]:
import sys t = float(sys.argv[1]) v0 = float(sys.argv[2]) g = 9.81 y = v0*t - 0.5*g*t**2 print (y)
Error handling
In [18]:
C = float(input('Enter degree Celsium:')) F = 9.0*C/5 + 32 print (F)
Enter degree Celsium:5 41.0
In [19]:
try: C = float(input('Enter degree Celsium:'))
except: print ('You failed to provide Celsius degrees as input on the command line!') sys.exit(1) # abort
F = 9.0*C/5 + 32 print ('%gC is %.1fF' % (C, F))
Enter degree Celsium:we You failed to provide Celsius degrees as input on the command line!
---------------------------------------------------------------------------
ValueError
Traceback (most recent call last)
in
1 try:
----> 2
C = float(input('Enter degree Celsium:'))
3 except:
ValueError: could not convert string to float: 'we'
During handling of the above exception, another exception occurred:
NameError
Traceback (most recent call last)
in
3 except:
4
print ('You failed to provide Celsius degrees as input on the co
mmand line!')
----> 5
sys.exit(1) # abort
6
7 F = 9.0*C/5 + 32
NameError: name 'sys' is not defined
In [ ]:
try: C = float(input('Enter degree Celsium:'))
except IndexError: print ('Celsius degrees must be supplied on the command line') sys.exit(1) # abort execution
except ValueError: print ('Celsius degrees must be a pure number, not "%s"' % C) sys.exit(1)
F = 9.0*C/5 + 32 print ('%gC is %.1fF' % (C, F))
examples of Errors in python
In [23]:
data = [1.0/i for i in range(1,10)] data[we]
---------------------------------------------------------------------------
NameError
Traceback (most recent call last)
in
1 data = [1.0/i for i in range(1,10)]
----> 2 data[we]
NameError: name 'we' is not defined
In [24]: C = float('21 C')
---------------------------------------------------------------------------
ValueError
Traceback (most recent call last)
in
----> 1 C = float('21 C')
ValueError: could not convert string to float: '21 C'
In [25]:
forr d in data: return data
File "", line 1 forr d in data: ^
SyntaxError: invalid syntax
3. Interface
In [26]: import tkinter tkinter._test()
................
................
In order to avoid copyright disputes, this page is only a partial summary.
To fulfill the demand for quickly locating and searching documents.
It is intelligent file search solution for home and business.
Related searches
- personality types 1 through 9
- personality test 1 through 9
- number generator 1 through 20
- biblical numbers 1 through 1000
- roman numerals 1 through 10
- 1 through 12 multiplication worksheets
- random number 1 through 5
- the amendments 1 through 27
- pick a number 1 through 7
- 2 1 relations and functions worksheet
- random number 1 through 3
- pick a number 1 through 11