0% found this document useful (0 votes)
9 views16 pages

Lecture 05

The document contains a series of Python code executions and their outputs, primarily focusing on user input validation for integers and marks, as well as age and membership status checks. It demonstrates error handling for invalid inputs and provides feedback based on user responses. Additionally, it includes examples of using the random module to generate random integers.

Uploaded by

nazish.z.nk
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views16 pages

Lecture 05

The document contains a series of Python code executions and their outputs, primarily focusing on user input validation for integers and marks, as well as age and membership status checks. It demonstrates error handling for invalid inputs and provides feedback based on user responses. Additionally, it includes examples of using the random module to generate random integers.

Uploaded by

nazish.z.nk
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 16

In [34]: %reset

Once deleted, variables cannot be recovered. Proceed


(y/[n])? y

In [35]: runfile('C:/Users/amajid/Downloads/
untitled3.py', wdir='C:/Users/amajid/Downloads')
plz enter an int: 4
the number is even

In [36]: runfile('C:/Users/amajid/Downloads/
untitled3.py', wdir='C:/Users/amajid/Downloads')
plz enter an int: 4.3
Traceback (most recent call last):

File "C:\Program
Files\Spyder\pkgs\spyder_kernels\py3compat.py", line
356, in compat_exec
exec(code, globals, locals)

File "c:\users\amajid\downloads\untitled3.py",
line 8, in <module>
x=int(input('plz enter an int: '))

ValueError: invalid literal for int() with base 10:


'4.3'

In [37]: '123'.isdigit()
Out[37]: True

In [38]: '123a'.isdigit()
Out[38]: False
1
In [39]: help(str.isdigit)
Help on method_descriptor:

isdigit(self, /)
Return True if the string is a digit string,
False otherwise.

A string is a digit string if all characters in


the string are digits and there
is at least one character in the string.

In [39]:

In [40]: runfile('C:/Users/amajid/Downloads/
untitled3.py', wdir='C:/Users/amajid/Downloads')
plz enter an int: 2.3
the entered input is invalid

In [41]: runfile('C:/Users/amajid/Downloads/
untitled3.py', wdir='C:/Users/amajid/Downloads')
plz enter an int: 4
Traceback (most recent call last):

File "C:\Program
Files\Spyder\pkgs\spyder_kernels\py3compat.py", line
356, in compat_exec
exec(code, globals, locals)

File "c:\users\amajid\downloads\untitled3.py",
line 15, in <module>
if x%2==0:

2
TypeError: not all arguments converted during string
formatting

In [42]: runfile('C:/Users/amajid/Downloads/
untitled3.py', wdir='C:/Users/amajid/Downloads')
plz enter an int: 3
the number is odd

In [43]: runfile('C:/Users/amajid/Downloads/
untitled3.py', wdir='C:/Users/amajid/Downloads')
plz enter an int: 6
the number is even

In [44]: runfile('C:/Users/amajid/Downloads/
untitled3.py', wdir='C:/Users/amajid/Downloads')
plz enter an int: fgfd
the entered input is invalid

In [45]: x='2'

In [46]: type(x)==int
Out[46]: False

In [47]: runfile('C:/Users/amajid/Downloads/
untitled3.py', wdir='C:/Users/amajid/Downloads')
plz enter your marks: 90
you get an A

In [48]: runfile('C:/Users/amajid/Downloads/
untitled3.py', wdir='C:/Users/amajid/Downloads')
plz enter your marks: 40
You get an F

3
In [49]: runfile('C:/Users/amajid/Downloads/
untitled3.py', wdir='C:/Users/amajid/Downloads')
plz enter your marks: 60.5
you get a D

In [50]: runfile('C:/Users/amajid/Downloads/
untitled2.py', wdir='C:/Users/amajid/Downloads')
plz enter your age: 50
plz enter your membership status(yes/no): No
no discount

In [51]: runfile('C:/Users/amajid/Downloads/
untitled2.py', wdir='C:/Users/amajid/Downloads')
plz enter your age: 40
plz enter your membership status(yes/no): yes
you get regular discount

In [52]: runfile('C:/Users/amajid/Downloads/
untitled2.py', wdir='C:/Users/amajid/Downloads')
plz enter your age: -3
plz enter your membership status(yes/no): yes
age is incorrect

In [53]: runcell(0, 'C:/Users/amajid/Downloads/


untitled2.py')
Out[53]: "\nage=eval(input('plz enter your age: '))
\n\nif age<=0:\n print('age is incorrect')
\n\nelse: \n membership=input('plz enter your
membership status(yes/no): ').lower()\n \n if
age<18:\n print('you get youth discount!')\n
\n elif age>=18 and age<65:\n if
membership=='yes':\n print('you get
regular discount')\n else:\n
print('no discount')\n \n elif age>65:\n
4
print('you get senior discount')\n\n"

In [54]: import random

In [55]: help(random)
Help on module random:

NAME
random

CLASSES
_random.Random(builtins.object)
Random
SystemRandom

class Random(_random.Random)
| Random(x=None)
|
| Random() -> create a random number generator
with its own internal state.
|
| Method resolution order:
| Random
| _random.Random
| builtins.object
|
| Methods defined here:
|
| __getstate__(self)
|
| __init__(self, x=None)
| Initialize self. See help(type(self))
for accurate signature.
|
5
| __reduce__(self)
| Helper for pickle.
|
| __setstate__(self, state)
|
| betavariate(self, alpha, beta)
|
| choice(self, seq)
|
| choices(self, population, weights=None, *,
cum_weights=None, k=1)
|
| expovariate(self, lambd)
|
| gammavariate(self, alpha, beta)
|
| gauss(self, mu, sigma)
|
| getstate(self)
| getstate() -> tuple containing the
current state.
|
| lognormvariate(self, mu, sigma)
|
| normalvariate(self, mu, sigma)
|
| paretovariate(self, alpha)
|
| randint(self, a, b)
|
| randrange(self, start, stop=None, step=1,
_int=<class 'int'>)
|
| sample(self, population, k)
6
|
| seed(self, a=None, version=2)
| seed([n]) -> None.
|
| Defaults to use urandom and falls back
to a combination
| of the current time and the process
identifier.
|
| setstate(self, state)
| setstate(state) -> None. Restores
generator state.
|
| shuffle(self, x, random=None)
|
| triangular(self, low=0.0, high=1.0,
mode=None)
|
| uniform(self, a, b)
|
| vonmisesvariate(self, mu, kappa)
|
| weibullvariate(self, alpha, beta)
|
|
----------------------------------------------------
------------------
| Class methods defined here:
|
| __init_subclass__(**kwargs) from
builtins.type
| This method is called when a class is
subclassed.
|
7
| The default implementation does nothing.
It may be
| overridden to extend subclasses.
|
|
----------------------------------------------------
------------------
| Data descriptors defined here:
|
| __dict__
| dictionary for instance variables (if
defined)
|
| __weakref__
| list of weak references to the object
(if defined)
|
|
----------------------------------------------------
------------------
| Data and other attributes defined here:
|
| VERSION = 3
|
|
----------------------------------------------------
------------------
| Methods inherited from _random.Random:
|
| __getattribute__(self, name, /)
| Return getattr(self, name).
|
| getrandbits(self, k, /)
| getrandbits(k) -> x. Generates an int
8
with k random bits.
|
| random(self, /)
| random() -> x in the interval [0, 1).
|
|
----------------------------------------------------
------------------
| Static methods inherited from
_random.Random:
|
| __new__(*args, **kwargs) from builtins.type
| Create and return a new object. See
help(type) for accurate signature.

class SystemRandom(Random)
| SystemRandom(x=None)
|
| Random() -> create a random number generator
with its own internal state.
|
| Method resolution order:
| SystemRandom
| Random
| _random.Random
| builtins.object
|
| Methods defined here:
|
| getrandbits(self, k)
| getrandbits(k) -> x. Generates an int
with k random bits.
|
| getstate = _notimplemented(self, *args,
9
**kwds)
|
| random(self)
| random() -> x in the interval [0, 1).
|
| seed(self, *args, **kwds)
| seed([n]) -> None.
|
| Defaults to use urandom and falls back
to a combination
| of the current time and the process
identifier.
|
| setstate = _notimplemented(self, *args,
**kwds)
|
|
----------------------------------------------------
------------------
| Methods inherited from Random:
|
| __getstate__(self)
|
| __init__(self, x=None)
| Initialize self. See help(type(self))
for accurate signature.
|
| __reduce__(self)
| Helper for pickle.
|
| __setstate__(self, state)
|
| betavariate(self, alpha, beta)
|
10
| choice(self, seq)
|
| choices(self, population, weights=None, *,
cum_weights=None, k=1)
|
| expovariate(self, lambd)
|
| gammavariate(self, alpha, beta)
|
| gauss(self, mu, sigma)
|
| lognormvariate(self, mu, sigma)
|
| normalvariate(self, mu, sigma)
|
| paretovariate(self, alpha)
|
| randint(self, a, b)
|
| randrange(self, start, stop=None, step=1,
_int=<class 'int'>)
|
| sample(self, population, k)
|
| shuffle(self, x, random=None)
|
| triangular(self, low=0.0, high=1.0,
mode=None)
|
| uniform(self, a, b)
|
| vonmisesvariate(self, mu, kappa)
|
| weibullvariate(self, alpha, beta)
11
|
|
----------------------------------------------------
------------------
| Class methods inherited from Random:
|
| __init_subclass__(**kwargs) from
builtins.type
| This method is called when a class is
subclassed.
|
| The default implementation does nothing.
It may be
| overridden to extend subclasses.
|
|
----------------------------------------------------
------------------
| Data descriptors inherited from Random:
|
| __dict__
| dictionary for instance variables (if
defined)
|
| __weakref__
| list of weak references to the object
(if defined)
|
|
----------------------------------------------------
------------------
| Data and other attributes inherited from
Random:
|
12
| VERSION = 3
|
|
----------------------------------------------------
------------------
| Methods inherited from _random.Random:
|
| __getattribute__(self, name, /)
| Return getattr(self, name).
|
|
----------------------------------------------------
------------------
| Static methods inherited from
_random.Random:
|
| __new__(*args, **kwargs) from builtins.type
| Create and return a new object. See
help(type) for accurate signature.

FUNCTIONS
betavariate(alpha, beta) method of Random
instance

choice(seq) method of Random instance

choices(population, weights=None, *,
cum_weights=None, k=1) method of Random instance

expovariate(lambd) method of Random instance

gammavariate(alpha, beta) method of Random


instance

13
gauss(mu, sigma) method of Random instance

getrandbits(k, /) method of Random instance


getrandbits(k) -> x. Generates an int with
k random bits.

getstate() method of Random instance


getstate() -> tuple containing the current
state.

lognormvariate(mu, sigma) method of Random


instance

normalvariate(mu, sigma) method of Random


instance

paretovariate(alpha) method of Random instance

randint(a, b) method of Random instance

random() method of Random instance


random() -> x in the interval [0, 1).

randrange(start, stop=None, step=1, _int=<class


'int'>) method of Random instance

sample(population, k) method of Random instance

seed(a=None, version=2) method of Random


instance
seed([n]) -> None.

Defaults to use urandom and falls back to a


combination
14
of the current time and the process
identifier.

setstate(state) method of Random instance


setstate(state) -> None. Restores generator
state.

shuffle(x, random=None) method of Random


instance

triangular(low=0.0, high=1.0, mode=None) method


of Random instance

uniform(a, b) method of Random instance

vonmisesvariate(mu, kappa) method of Random


instance

weibullvariate(alpha, beta) method of Random


instance

DATA
__all__ = ['Random', 'seed', 'random',
'uniform', 'randint', 'choice',...

FILE
c:\program
files\spyder\python\python38.zip\random.py

In [56]: import random

In [57]: from random import randint


15
In [58]: randint(1,10)
Out[58]: 4

In [59]: randint(1,10)
Out[59]: 3

In [60]: randint(1,10)
Out[60]: 1

In [61]: randint(1,10)
Out[61]: 8

In [62]: randint(1,10)
Out[62]: 6

In [63]: runfile('C:/Users/amajid/Downloads/
untitled2.py', wdir='C:/Users/amajid/Downloads')
rock

In [64]: runfile('C:/Users/amajid/Downloads/
untitled2.py', wdir='C:/Users/amajid/Downloads')
rock

In [65]: runfile('C:/Users/amajid/Downloads/
untitled2.py', wdir='C:/Users/amajid/Downloads')
scissor

In [66]:

16

You might also like