PYTHON3 PLEASE! Overview: We will create a program that prints numbers as Roman
ID: 3730490 • Letter: P
Question
PYTHON3 PLEASE!
Overview: We will create a program that prints numbers as Roman Numerals.
You can read more about how Roman Numerals work on Wikipedia https://en.wikipedia.org/wiki/Roman_numerals
Roman Numerals use letters to represent numbers.
1 is I
5 is V
10 is X
50 is L
100 is C
500 is D
1,000 is M
5,000 is v (really capital v with a line, but we can't print that)
10,000 is x (really capital x with a line)
Other numbers are made with a combination of these letters. For example, 3 is III.
Note: Numbers greater then 3,999 require symbols with lines over letters. We can't use these, so use lower case v for 5000 and x for 10000.
The first 10 numbers are
I
II (1+1=2)
III (1+1+1=3)
IV (5-1=4)
V
VI (5+1=6)
VII (5+2=7)
VIII (5+3=8)
IX (10-1=9)
X
This pattern repeats for larger numbers. For example to create 47, we need to figure out 40 first. 4 was IV, there are equivalent 1, 5, and 10 symbols for the tens place. We can determine that 40 is XL since X is 10 and L is 50. We then combine this with 7 from above to get XLVII. Every value can be broken down into this basic 1-9 pattern just using different symbols.
(1) Create a function roman(num, one, five, ten) that takes a number between 1 and 9. The function returns a string with the roman representation using the strings given for the one, five, and ten symbols. If a number outside the range is given return the empty string "".
For example, Executing roman(4,"X","L","C") should return "XL"
(2) Create a function roman_num(num) that takes a number between 1 and 9,999 and returns it as a Roman Numeral. Return an empty string outside the range. Use the function from the previous step.
For example, executing roman_num(47) should return "XLVII".
(3) Create a main program that asks users for a number and prints it as a Roman Numeral. The program should run until the user enters 0. Use the functions from the previous steps. Print "Bye." when you exit. Remember to use if name=="main": so ZyBooks can test your functions.
Here is an example execution trace.
--------------------------------------------------------------------------
--------------------------------------------------------
I believe my code is correct but I am not getting the correct output. ZyBooks is very sensitive and I'm not sure where to fix in my code
Below is my code:
import sys
from collections import OrderedDict
roman = OrderedDict()
def Roman():
roman[1000] = "M"
roman[500] = "D"
roman[100] = "C"
roman[50] = "L"
roman[10] = "X"
roman[5] = "V"
roman[1] = "I"
def roman_num(num):
if num >= 1 and num <= 9999:
Roman()
for r in roman.keys():
x,y = divmod(num, r)
yield roman[r] * x
num -= (r * x)
if num > 0:
roman_num(num)
else:
break
else:
num=""
return 'Roman Numerals:',num
if __name__ == "__main__":
print("Roman Number Generator. Enter 0 to quit.")
num = int(input("Enter a number between 1 and 9,999:"))
if num == 0:
print("Bye.")
sys.exit()
else:
print("".join([a for a in roman_num(num)]))
here is the output problems i am experiencing
Explanation / Answer
As I checked, your whole logic is correct and you made great effort.
To work properly, only thing you need to do is to add some keys in your Roman function like this:
def Roman():
roman[1000] = "M"
roman[900] = "CM"
roman[500] = "D"
roman[400] = "CD"
roman[100] = "C"
roman[90] = "XC"
roman[50] = "L"
roman[40] = "XL"
roman[10] = "X"
roman[9] = "IX"
roman[5] = "V"
roman[4] = "IV"
roman[1] = "I"
Thank You
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.