[Help] Convert integer to Roman numerals? {Screenshot attached} - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: General Coding Help (https://python-forum.io/forum-8.html) +--- Thread: [Help] Convert integer to Roman numerals? {Screenshot attached} (/thread-12041.html) Pages:
1
2
|
[Help] Convert integer to Roman numerals? {Screenshot attached} - vanicci - Aug-06-2018 [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/28777219/basic-program-to-convert-integer-to-roman-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: ===================================================================Any advice is much appreciated! :) Thank you in advance! RE: [Help] Convert integer to Roman numerals? {Screenshot attached} - volcano63 - Aug-06-2018 For starters, you need a dict - not list of tuplesnum_map = {1000: 'M', 500: 'D', 100: 'C', 50: 'L', 10: 'X', 5: 'V', 1: 'I'} RE: [Help] Convert integer to Roman numerals? {Screenshot attached} - Windspar - Aug-06-2018 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. RE: [Help] Convert integer to Roman numerals? {Screenshot attached} - volcano63 - Aug-06-2018 (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 .Really bright idea - propose un-Pythonic complicated mapping instead of standard dictionary RE: [Help] Convert integer to Roman numerals? {Screenshot attached} - ichabod801 - Aug-06-2018 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. RE: [Help] Convert integer to Roman numerals? {Screenshot attached} - Windspar - Aug-06-2018 @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. RE: [Help] Convert integer to Roman numerals? {Screenshot attached} - vanicci - Aug-06-2018 @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] RE: [Help] Convert integer to Roman numerals? {Screenshot attached} - ichabod801 - Aug-06-2018 You need to print the result for it to show up on the screen. Change your last line to: print(int_to_roman(num)) RE: [Help] Convert integer to Roman numerals? {Screenshot attached} - Windspar - Aug-06-2018 Try print(int_to_roman(int(result))) RE: [Help] Convert integer to Roman numerals? {Screenshot attached} - ichabod801 - Aug-06-2018 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. |