Question
Defining procedures and calling them from the main program. Define a procedure named getNameAndGreet(), that asks an user to input their name and print it
Defining procedures and calling them from the main program.
Define a procedure named getNameAndGreet(), that asks an user to input their name and print it along with Hello, .
In the same file, include a statement to call the procedure getNameAndGreet(). Execute the program.
In the procedure getNameAndGreet(), include a statement to print the number of characters in the name entered. For example, if name = Hannah Clarke, the program prints, Your name has 13 characters.
Execute the modified program.
Define functions and call them.
In the first topic, Introduction to Python, we created a simple program that converts temperature in Celsius to temperature in Fahrenheit. In the blank trinket given below, write a function named tempConversion, that takes in temperature in Celsius and returns temperature in Fahrenheit.
[tempF = tempC * 1.8 + 32]
In the same file, include statements to get user input, call function tempConversion() and print the result returned by function.
Modify the function tempConversion() such that it takes two parameters: temperature and a string variable. Users may enter temperatures in Celsius or Fahrenheit. If the string variable = F, then the temperature entered is in Fahrenheit and is converted to Celsius. If the string variable = C, then temperature entered is in Celsius and is converted to Fahrenheit.
[tempC = (tempF - 32) * 5/9]
Include the function call statement to call the function tempConversion() and execute the program.
Defining functions and calling them.
In the blank trinket given below, define a function that takes in two numbers and returns the smallest of two numbers.
In the same trinket file, write statements to get two numbers, call the function and print the result.
Using the function defined in (i), include statements to find the smallest of three numbers entered by the user.
Defining functions that return multiple values.
In the blank trinket given below, define a function that takes in an array and calculates the total and average of numbers present in the array. The function also finds out the largest and smallest number in the array. The function returns the four values: total, average, largest number and smallest number.
In the same trinket file, write statements to input 10 numbers into an array. Call the function defined in (i) and print the values returned by the function.
Use of global variables.
Execute the code given in the trinket to understand how variables within a subroutine works. Variables created within a subroutine are local to it unless they are defined using global keywords.
def myproc():
x=2
y=3
print("Values of x and y inside myproc():", x,y)
#main program
x=5
y=9
print("Values of x and y before calling myproc():", x,y)
myproc()
print("Values of x and y:", x,y)
The variable x in myproc() is modified with a global keyword. Execute the code given and observe the output.
def myproc():
global x
x=2
y=3
print("Values of x and y inside myproc():", x,y)
#main program
x=5
y=9
print("Values of x and y before calling myproc():", x,y)
myproc()
print("Values of x and y:", x,y)
Recursion
Fibonacci sequence is the sequence: 0, 1, 1, 2, 3, 5, 8 and so on. The first two numbers are 0 and 1. The other terms are obtained by adding the preceding two terms. Using recursion, write a program for printing the Fibonacci sequence based on the number entered by the user. For example, if the user inputs 10, the program prints the first 10 terms of Fibonacci sequence.
Using datetime library
Write a Python program to print the current date. Store current date in a variable named date1.
Include the statements given below in your program.
date2 = date1 + timedelta(15)
print(date2)
Execute the program to understand the function of timedelta() function.
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started