Levels Tought:
Elementary,Middle School,High School,College,University,PHD
Teaching Since: | Jul 2017 |
Last Sign in: | 271 Weeks Ago, 3 Days Ago |
Questions Answered: | 1850 |
Tutorials Posted: | 1850 |
Graduate in Biology and Nutrition, MBA Finance
Florida State University
Aug-2000 - Jul-2007
Ass. Relationship Manager
Penn-Florida
Mar-2009 - Feb-2016
Roman Numeral Lab
Python Conditionals and Functions
Part 1:
1)Â Write a Python program, that prompts the user to enter a number within the range
of 1 through 10. The program will then display the Roman numeral version of that
number. if the number is outside the range of 1 through 10, the program should display
an error message. Roman Numerals:
 1 = I
 2 = II
 3 = III
 4 = IV
 5 = V
 6 = VI
 7 = VII
 8 = VIII
 9 = IX
 10 = X
This is what I have so far for Part 1! I get error as the output.
def main():
number = input('Enter a number within the range of 1 through 10: ')
if number == 1:
roman_num = 'I'
elif number == 2:
roman_num = 'II'
elif number == 3:
roman_num = 'II.'
elif number == 4:
roman_num = 'IV'
elif number == 5:
roman_num = 'V'
elif number == 6:
roman_num = 'VI'
elif number == 7:
roman_num = 'VII'
elif number == 8:
roman_num = 'VIII'
elif number == 9:
roman_num = 'IX'
elif number == 10:
roman_num = 'X'
else:
roman_num = 'Error!'
main()
Part 2: Defining Functions
Modify the program to use functions.Â
1) Use Save As to change the name of your program so you won't overwrite the Part 1
program.
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 the
way to the left
main()Â Â Â Â Â Â
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 number
Modify the main() function to call getInteger() rather than getting the number directly
from the user using:Â Â Â
number = getInteger()
If necessary, replace the variable number with whatever variable you were using to hold
the 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(), add
def 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 works
Â
Â
Â
Â
Random Star Patterns
Instructions:
Â
1) Write a program that asks the user for a number (integers between 1 and 15, then uses a loop to
Â
display the same number of adjacent stars (asterisks). Put this in a loop that repeats 5 times. For
Â
example, 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. Add
Â
this to the top of the file (just below the header comment)
Â
import random
Â
Now rather than getting each of the five numbers from the user, get them using
Â
number = random.randint(1, 50)
Â
Change the program so it prints 50 lines of stars instead of 5
Hel-----------lo -----------Sir-----------/Ma-----------dam----------- Â----------- -----------Tha-----------nk -----------you----------- fo-----------r u-----------sin-----------g o-----------ur -----------web-----------sit-----------e a-----------nd -----------acq-----------uis-----------iti-----------on -----------of -----------my -----------pos-----------ted----------- so-----------lut-----------ion-----------.Pl-----------eas-----------e p-----------ing----------- me----------- on----------- ch-----------at -----------I a-----------m Â----------- on-----------lin-----------e o-----------r i-----------nbo-----------x m-----------e a----------- me-----------ssa-----------ge -----------I