Defining Functions

by | Sep 20, 2021 | Homework Help

roman_num = ‘Error!’main()Part 2: Defining FunctionsModify the program to use functions. 1) Use Save As to change the name of your program so you won’t overwrite the Part 1program.2) Define a main function by adding this before the first line of your program         def main()    Now indent your entire program, selecting everything below def main(), and choosing”Indent Region” from the format menu.Now, add the call to main() at the end. Note this is NOT indented.  It must be all theway to the leftmain()      Run your program to be sure it still works.3) Define a getInteger() function to get the number from the user.Add it on the next to the last line of the program, right above the call to main()def getInteger()number = int(input(“Enter a number between 1 and 10: “))return numberModify the main() function to call getInteger() rather than getting the number directlyfrom the user using:   number = getInteger()If necessary, replace the variable number with whatever variable you were using to holdthe number.Run your program to be sure it still works.4) Next put all the computation into its own function.On the next to the last line of the program, right above the call to main(), adddef printRomanNumeral(number)Cut all of the computation from your main() function and paste it here.Underneath the call to getInteger()  in your main function, enter          printRomanNumeral(number).  At this point your main function should be just two lines – something like this:def main()       number = getInteger()       printRomanNumeral(number)Test the program to be sure it still worksRandom Star PatternsInstructions:1) Write a program that asks the user for a number (integers between 1 and 15, then uses a loop todisplay the same number of adjacent stars (asterisks). Put this in a loop that repeats 5 times. Forexample, if user enters 5, 9, 3, 11, and 8, your output should look like:How many? 5*****How many? 9*********How many? 3***How many? 11***********How many? 8********2)  Modify the program to use random numbers instead of getting numbers from the user. Addthis to the top of the file (just below the header comment)import randomNow rather than getting each of the five numbers from the user, get them usingnumber = random.randint(1, 50)Change the program so it prints 50 lines of stars instead of 5import randomfor i in range(50):number = random.randint(1, 50)for j in range(number):print(‘*’, end = ”)print(”)

We help you get better grades, improve your productivity and get more fun out of college!!

Get Homework Answer for this question

Free title page

Free reference page

Free formatting

Unlimited revisions

Get answer to your homework