Chapter 3 12
Chapter 3 12
Questions
Question 1
Solution
def convert_dollars_to_rupees_void(amount_in_dollars,
conversion_rate):
amount_in_rupees = amount_in_dollars * conversion_rate
print("Amount in rupees:", amount_in_rupees)
Output
Question 2
Solution
default_volume = calculate_volume()
print("Volume of the box with default values:", default_volume)
v = calculate_volume(10, 7, 15)
print("Volume of the box with default values:", v)
b = calculate_volume(width = 19)
print("Volume of the box with default values:", b)
Output
Question 3
Write a program to have following functions :
(ii) a function that takes two char arguments and returns True if
both the arguments are equal otherwise False.
Solution
calculate_cube(3)
calculate_cube()
char1 = 'a'
char2 = 'b'
print("Characters are equal:", check_equal_chars(char1, char1))
print("Characters are equal:", check_equal_chars(char1, char2))
Output
Cube of 3 is 27
Cube of 2 is 8
Characters are equal: True
Characters are equal: False
Question 4
Write a function that receives two numbers and generates a
random number from that range. Using this function, the main
program should be able to print three numbers randomly.
Solution
import random
for i in range(3):
random_num = generate_random_number(num1, num2)
print("Random number between", num1, "and", num2, ":",
random_num)
Output
Question 5
Solution
def same_length_strings(str1, str2):
return len(str1) == len(str2)
s1 = "hello"
s2 = "world"
s3 = "python"
print(same_length_strings(s1, s2))
print(same_length_strings(s1, s3))
Output
True
False
Question 6
Solution
result = nthRoot(x, n)
print("The", n, "th root of", x, "is:", result)
default_result = nthRoot(x)
print("The square root of", x, "is:", default_result)
Output
Solution
import random
def generate_number(n):
lower_bound = 10 ** (n - 1)
upper_bound = (10 ** n) - 1
return random.randint(lower_bound, upper_bound)
Output
Question 8
Write a function that takes two numbers and returns the number
that has minimum one's digit.
[For example, if numbers passed are 491 and 278, then the function
will return 491 because it has got minimum one's digit out of two
given numbers (491's 1 is < 278's 8)].
Solution
Output
Question 9
Solution
Output