In: Computer Science
Exercise 1. Write a function named printBox to print a 5 x 10 box of asterisks using nested loops. When printBox() is called your function should display the pattern shown below.
Write a function named printBox to print a 5 x 10 box of
asterisks
using nested loops. When printBox() is called your function
should
display the following information.
**********
**********
**********
**********
**********
"""
# place the code for the printBox function here
printBox( numRows, numCols )
Exercise 2. Write a function to print a m x n box of asterisks using nested loops where m represents the number of rows and n represents the number of columns.
Write a function to print a m x n box of asterisks using nested
loops where
m represents the number of rows and n represent the number of
columns.
If the user enters 3 for the number of rows and 4 for the number
of columns,
when printBox( numRows, numCols ) is called your function should
display
the following:
****
****
****
"""
# place the code for the printBox function here
numRows = int( input( 'Enter the number of rows: ') )
numCols = int( input( 'Enter the number of columns: ') )
printBox( numRows, numCols )
. For example, if the user enters 3 for the number of rows and 4 for the number of columns when printBox( numRows, numCols ) is called your function should display the following:
****
****
****
Exercise 3. Complete the convertTemperture function in exercise3.py by replacing the pass statement with code to compute the temperature conversion and return the converted Celsius value when convertTemperature( tempInF ) is called. Recall you wrote an expression to perform temperature conversion in laboratory assignment 2. The mathematical formula for temperature conversion is: ?? = 5 9 ∙ (?? − 32) where F is the temperature in Fahrenheit and C is the temperature in Celsius. Use floating-point values in your calculation.
def convertTemperature( fahrenheit ):
pass
tempInF = float( input( "Enter a temperature in Fahrenheit:
"))
tempInC = convertTemperature( tempInF )
print( f'{tempInF:.1f} Fahrenheit is {tempInC:.1f} in Celsius' )
Exercise 4. Write a function that prompts the user to enter x and y values of a point in a 2-D coordinate system. Your function should return the two values as the values of the function. This source code calls your function to get values for x and y, and then display the coordinate value. After you add the function definition for getCoordinate.
def getCoordinate():
pass
# these lines call your function to get values of the
# coordinates and displays the result
x, y = getCoordinate()
print ( f'You entered the coordinate <{x},{y}>' )
Exercise 1:
def printBox():
for i in range(5):
for j in range(10):
print("*", end =" ")
print()
printBox()
Exercise 2:
def printBox(numRows,numColumns):
for i in range(numRows):
for j in range(numColumns):
print("*", end ="")
print()
numRows = int( input( 'Enter the number of rows: ') )
numCols = int( input( 'Enter the number of columns: ') )
printBox( numRows, numCols )
Output:
Exercise 3:
def convertTemperature( fahrenheit ):
celsius = (fahrenheit - 32) * 5.0/9.0
return celsius
tempInF = float( input( "Enter a temperature in Fahrenheit: "))
tempInC = convertTemperature( tempInF )
print( f'{tempInF:.1f} Fahrenheit is {tempInC:.1f} in Celsius' )
Output:
Exercise 4:
def getCoordinate():
x = float( input( "Enter x coordinate: "))
y = float( input( "Enter y coordinate: "))
return x,y
x, y = getCoordinate()
print ( f'You entered the coordinate <{x},{y}>' )
Output :