Roman Numeral Lab




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’
roman_num = ‘Error!’


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


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


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


At this point your main function should be just two lines – something like this:

def main()

number = getInteger()


Test the program to be sure it still works

Random Star Patterns


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