Python Forum
[Help] Convert integer to Roman numerals? {Screenshot attached}
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[Help] Convert integer to Roman numerals? {Screenshot attached}
#1
Question 
[Help] Convert integer to Roman numerals? {Screenshot attached}

Hi,

Please see attached screenshot for the current exercise that I'm trying to execute?

[Image: 060818_zps7cdg6nrb.png]

Tbh, I am also unsure with my code. I gathered the idea from here (https://stackoverflow.com/questions/2877...n-numerals)

===================================================================
Here's my currentcode:
print("[System] Hi! Enter any number between 1 and 3000 so the program will show you the old-school Roman numeral. In other words, 4 should return 'IIII'. Ready?!")

num_map = [(1000, 'M'), (500, 'D'), (100, 'C'), (50, 'L'), (10, 'X'), (5, 'V'), (1, 'I')]
num = int(input("Enter any number between 1 and 3000: "))

def num2roman(num):

    while num > 0:
        for key, val in num_map:
            while num >= 1:
                print(num_map.values())
num2roman(num)
===================================================================
Error:
Error:
Traceback (most recent call last): File "soc-wk1day4h-cert-vanessa-dunford.py", line 25, in <module> num2roman(num) File "soc-wk1day4h-cert-vanessa-dunford.py", line 24, in num2roman print(num_map.values()) AttributeError: 'list' object has no attribute 'values'
===================================================================

Any advice is much appreciated! :)

Thank you in advance!
Blockchain Visionary & Aspiring Encipher/Software Developer
me = {'Python Learner' : 'Beginner\'s Level'}
http://bit.ly/JoinMeOnYouTube
Reply
#2
For starters, you need a dict - not list of tuples
num_map = {1000: 'M', 500: 'D', 100: 'C', 50: 'L', 10: 'X', 5: 'V', 1: 'I'}
Test everything in a Python shell (iPython, Azure Notebook, etc.)
  • Someone gave you an advice you liked? Test it - maybe the advice was actually bad.
  • Someone gave you an advice you think is bad? Test it before arguing - maybe it was good.
  • You posted a claim that something you did not test works? Be prepared to eat your hat.
Reply
#3
List of tuples are fine. Just need to expand it.
roman_map = [(1000, "M"), (900, "CM"), (500, "D"), (400, "CD"), (100, "C"),
    (90, "XC"), (50, "L"), (40, "XL"), (10, "X"), (9, "IX"), (5, "V"),
    (4, "IV"), (1, "I")]
Only have 1 loop.
def num2roman(num):
    while num > 0:
keep track of roman numeral
def num2roman(num):
    numeral = ""
    while num > 0:
keep track where at in list.
def num2roman(num):
    numeral = ""
    pos = 0
    while num > 0:
in loop use if block to see if num > then value and - value from num. Else increase pos.
99 percent of computer problems exists between chair and keyboard.
Reply
#4
(Aug-06-2018, 02:07 PM)Windspar Wrote: List of tuples are fine. Just need to expand it.

I don't know what they thought when they created dict in Python Naughty .

Really bright idea - propose un-Pythonic complicated mapping instead of standard dictionary Wall
Test everything in a Python shell (iPython, Azure Notebook, etc.)
  • Someone gave you an advice you liked? Test it - maybe the advice was actually bad.
  • Someone gave you an advice you think is bad? Test it before arguing - maybe it was good.
  • You posted a claim that something you did not test works? Be prepared to eat your hat.
Reply
#5
The tuples are better than a dict. You need to subtract the numbers in order, and the list of tuples provides the order. I think it would be more pythonic to loop through the tuples rather than keep track of an index, but a dictionary is not the proper tool here.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#6
@volcano63
Walking through a list of tuples seem pretty elegant.
Since it not jumping around the list.

Saying something is un-Pythonic. Doesn't make it so.

Changing list to a tuple probably be better. Just because it keeps it all immutable.
roman_map = ((1000, "M"), (900, "CM"), (500, "D"), (400, "CD"), (100, "C"),
    (90, "XC"), (50, "L"), (40, "XL"), (10, "X"), (9, "IX"), (5, "V"),
    (4, "IV"), (1, "I"))
But not a must.
99 percent of computer problems exists between chair and keyboard.
Reply
#7
@volcano63 & @ichabod801 = Thank you very much for your input! :)

@Windspar, can I please ask you to look into my current code?

When user input's integer, there is no roman numeral coming up?

print("[System] Hi! Enter any number between 1 and 3000 so the program will show you the old-school Roman numeral. In other words, 4 should return 'IIII'. Ready?!")

num_map = [(1000, 'M'), (900, 'CM'), (500, 'D'), (400, 'CD'), (100, 'C'), (90, 'XC'),
           (50, 'L'), (40, 'XL'), (10, 'X'), (9, 'IX'), (5, 'V'), (4, 'IV'), (1, 'I')]

result = int(input("Enter any number between 1 and 3000: "))
num = 0

def int_to_roman(num):
    result = []
    for (arabic, roman) in num_map:
        (factor, num) = divmod(num, arabic)
        result.append(roman * factor)
        if num == 0:
            break
    return "".join(result)

int_to_roman(num)
===================================================================
My Terminal's Screenshot:

[Image: 060818_zpsmx8wa1rw.png]
Blockchain Visionary & Aspiring Encipher/Software Developer
me = {'Python Learner' : 'Beginner\'s Level'}
http://bit.ly/JoinMeOnYouTube
Reply
#8
You need to print the result for it to show up on the screen. Change your last line to:

print(int_to_roman(num))
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#9
Try
print(int_to_roman(int(result)))
99 percent of computer problems exists between chair and keyboard.
Reply
#10
She's covering using the same numeral multiple times with the divmod. Note to OP: that integer division (//) will give you the integer portion without the remainder, which you aren't using.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  How to convert 4 bytes to an integer ? GiggsB 11 6,880 Jan-20-2022, 03:37 AM
Last Post: GiggsB
  Help with implementation of the logic to convert roman numeral to integer jagasrik 2 2,322 Aug-14-2020, 07:31 PM
Last Post: deanhystad
  Converting number to roman numerals jagasrik 6 3,619 Aug-11-2020, 03:47 AM
Last Post: voidptr
  IEDriverServer screenshot ABVSVL 0 1,735 Jul-12-2020, 09:31 AM
Last Post: ABVSVL
  pyautogui.screenshot region is not working alexlu 6 6,522 Jun-04-2020, 10:46 AM
Last Post: alexlu
  What is the best way to search for a pixel in a screenshot? TheZadok42 1 2,611 May-15-2020, 12:37 PM
Last Post: scidam
  Spawning a new process that is not attached to python cman234 3 1,908 Apr-25-2020, 05:24 PM
Last Post: cman234
  Roman Numeral Ordered List? (first post) CopperyMarrow15 1 1,728 Nov-06-2019, 11:06 PM
Last Post: Larz60+
  My objective is to get the shape parameters of the particles in attached image chad221 0 1,787 Oct-26-2019, 10:27 AM
Last Post: chad221
  Want to take a Screenshot from a File in Linux dhiliptcs 2 2,565 Oct-21-2019, 01:22 PM
Last Post: dhiliptcs

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020