##4. What will the following program display?
##def main():
## x = 1
## y = 3.4
## print(x, y) ## first printing
## change_us(x, y)
## print(x, y) ##second printing
##
##def change_us(a, b):
## a = 0
## b = 0
## print(a, b)
##
##main()
##
##Yes, yes, main() displays
##1 3.4
##0 0
##1 3.4
## The question is: why x and y are still the same while the second printing of
(x,y)?
## It seems that both x and y went through change_us() subprogram and change_us()
## makes zeros from its arguments?
##Explain, please.
##5. Look at the following function definition:
##def my_function(a, b, c):
## return a + (b - c)
##a. Write a statement that calls this function and uses keyword arguments
##to pass 2 into b and 3 into c.
##b. What will be printed when the function call my_function(3.5) executes?
##6. Write a statement that generates a random number in the range of 1 through 100
and
##assigns it to a variable named rand.
##Solution:
##import random
##rand = random.randint(1, 100)
##6.1 further exension.
## a) Write a function RANDO(n) which returns a raw of n(independent)
##random numbers from 1 to 100. Use random.seed(10).
##Let n is always to be integer and > 1 (don't check that)
## hint:
##def random_raw(n):
## import random
## random.seed(10)
## for m in .......:
## print(random.randint(1, 100), ....)
## b) Call your function for n=3
##7. The following statement calls a function named half, which returns a value
##that is half that of the argument. (Assume the number variable references a float
value.)
##Write code for the function.
##result = half(number)
##Solution:
##def half(value):
## return value / 2.0
##7.1 Further extension. Write a program value_part() which returns the
##pth part (as float) of the value (value and p are integer).
##Let get value and p as input. Format the result as .2f.
##Hint:
##def value_part():
## value = int(input('put the value: '))
## p = ...(...('put the p: '))
## return ...(.../..., '.2f')
4
x and y are still the same while the second printing of (x,Y) because both x and y are declared inside the main method and
we are printing x and y inside the main only .and when we call change_us method then the value of a=x=1 and b=y=3.4
but in change_us method we modify a and b that a=0 b=0 so print(a,b) after change of a and b will print 0 0
here no modification is done regarding to the variable x and y so it will remain same as previous value .
so the output
1 3.4
0 0
1 3.4
5.
x=my_function(1,2,3)
here value of a=1,b=2 and c=3 the return value is stored in x
b) compilation error
6
Get Answers For Free
Most questions answered within 1 hours.