Home > database >  Python - Transform an integer to a roman
Python - Transform an integer to a roman

Time:11-01

The code is working fine at a glance. But when I input any number > 3999 the code ends with the message "Invalid number. Try again!" and the input not repeat as it is invalid. The input should back again and again until the input number is valid (within 1 to 3999).

Here is my code:

def int_to_roman(num):
    val = [
            1000, 900, 500, 400,
            100, 90, 50, 40,
            10, 9, 5, 4,
            1
            ]
    syb = [
            "M", "CM", "D", "CD",
            "C", "XC", "L", "XL",
            "X", "IX", "V", "IV",
            "I"
            ]
    roman_num = ''
    i = 0
    while  num > 0:
        for _ in range(num // val[i]):
            roman_num  = syb[i]
            num -= val[i]
        i  = 1
    return roman_num
    
def roman_to_int(numeral):
    rom_val = {'I': 1, 'V': 5, 'X': 10, 'L': 50, 'C': 100, 'D': 500, 'M': 1000}
    int_val = 0
    for i in range(len(numeral)):
        if i > 0 and rom_val[numeral[i]] > rom_val[numeral[i - 1]]:
            int_val  = rom_val[numeral[i]] - 2 * rom_val[numeral[i - 1]]
        else:
            int_val  = rom_val[numeral[i]]
    return int_val

while True:
    try:
        message = int(input("Enter your integer number: "))
    except ValueError:
        print("Invalid input. Try again!")
        continue
    else:
        break
if message == 0:
    print ("Invalid integer number. Try Again!")
elif message <= 3999:
    print ('Integer to Roman: ',int_to_roman (message))
else:
    print("Invalid number. Try again!") 

CodePudding user response:

while True:
    try:
        message = int(input("Enter your integer number: "))
    except ValueError:
        print("Invalid input. Try again!")
        continue
    if message == 0:
        print ("Invalid integer number. Try Again!")
    elif message <= 3999:
        print ('Integer to Roman: ',int_to_roman (message))
    else:
        print("Invalid number. Try again!") 

I think removing the else:break(it is not necessary in this situation) and indenting the last if else block can fix this issue

CodePudding user response:

Your if conditions are outside the while loop ,and the else break is not necessary ( you can if you want add break in the second if condition ).

def int_to_roman(num):
    val = [
            1000, 900, 500, 400,
            100, 90, 50, 40,
            10, 9, 5, 4,
            1
            ]
    syb = [
            "M", "CM", "D", "CD",
            "C", "XC", "L", "XL",
            "X", "IX", "V", "IV",
            "I"
            ]
    roman_num = ''
    i = 0
    while  num > 0:
        for _ in range(num // val[i]):
            roman_num  = syb[i]
            num -= val[i]
        i  = 1
    return roman_num
    
def roman_to_int(numeral):
    rom_val = {'I': 1, 'V': 5, 'X': 10, 'L': 50, 'C': 100, 'D': 500, 'M': 1000}
    int_val = 0
    for i in range(len(numeral)):
        if i > 0 and rom_val[numeral[i]] > rom_val[numeral[i - 1]]:
            int_val  = rom_val[numeral[i]] - 2 * rom_val[numeral[i - 1]]
        else:
            int_val  = rom_val[numeral[i]]
    return int_val

while True:
    try:
        message = int(input("Enter your integer number: "))
    except ValueError:
        print("Invalid input. Try again!")
        continue
    if message == 0:
        print ("Invalid integer number. Try Again!")
    elif message <= 3999:
        print ('Integer to Roman: ',int_to_roman (message))
        break
    else: 
        print("Invalid number. Try again!")  

CodePudding user response:

Based on the indentation of your code, it seems that checking the message value range is performed outside of your while loop. This will not allow the input to be requested again.

To make this cleaner, you should give a more purposeful name to your variable (number) and only break out of the loop when you get a valid value in it. Then you can place the roman numeral conversion outside of the loop with the assurance that the number is valid.

while True:
    try:
        number = int(input("Enter your integer number: "))
        if number > 0 and number < 4000: break # number is valid           
    except ValueError: pass
    print("Invalid number. Try again!")
print ('Integer to Roman: ',int_to_roman (number))
  • Related