-1

It prints only the second command in if and it doesnt make the first one if the input is decimal . i checked the 2 codes seperated and they work perfect. i just want to print decimal if its roman and roman if its decimal

roman_to_decimal = { 'I': 1, 'V': 5, 'X': 10, 'L': 50, 'C': 100, \
                         'D': 500, 'M': 1000 }
    def int2roman(numberdec):
        numerals={1:"I", 4:"IV", 5:"V", 9: "IX", 10:"X", 40:"XL", 50:"L",
                  90:"XC", 100:"C", 400:"CD", 500:"D", 900:"CM", 1000:"M"}
        result=""
        for value, numeral in sorted(numerals.items(), reverse=True):
            while numberdec >= value:
                result += numeral
                numberdec -= value
        return result


numberchk=(input("Enter a Roman numeral or a Decimal numeral:" ))
  ##here is the problem i get         
if  numberchk==int :
        print (int2roman(int(numberchk)))
        sys.exit()
else :
    roman=numberchk

converted = True
number = 0

for n in range(0, len(roman)-1):
    this_char = roman[n]
    next_char = roman[n+1]
    if (this_char in roman_to_decimal) and \
       (next_char in roman_to_decimal):

        this_number = roman_to_decimal[this_char]
        next_number =  roman_to_decimal[next_char]

        if this_number < next_number:
            number -= this_number
        else:
            number += this_number



if converted:
    ##  add last roman numeral
    number += roman_to_decimal[roman[len(roman)-1]]

    print ("\nThe roman numeral", roman, "is equal to",number)
  • 1
    *What* error do you get? What is the full traceback? – Martijn Pieters Feb 24 '14 at 13:26
  • 3
    `numberchk==int` is **never** True. What do you want to test there? – Martijn Pieters Feb 24 '14 at 13:26
  • Did you want to test if `numberchk` might be a number perhaps? Then you are going about it the wrong way, I am afraid. – Martijn Pieters Feb 24 '14 at 13:27
  • actually in if command where i check in mumberchk==int it print me only the else command whatever the input is and if input its decimal and not roman number it gets me error – user3346713 Feb 24 '14 at 13:28
  • 1
    @devnull: `input()` in Python 3 always returns a string, so the OP wants to know if the string value contains a decimal number. I think the dupe I found will be more helpful here. – Martijn Pieters Feb 24 '14 at 13:30
  • You already asked this [in an earlier question](http://stackoverflow.com/questions/21988366/even-if-the-first-command-in-if-is-true-it-doesnt-print-what-i-want-it-only-p) and got good answers there. – poke Feb 24 '14 at 13:36

1 Answers1

0

Your line

if  numberchk==int :

does not appear to be correct. You should use

try:
  decval = int(numberchk)
  romanval = int2roman(decval)
  #Continue with other processing here
except ValueError:
  # Verify that it is a legal roman numeral
  romanval = numberchk
  decval = roman2int(numberchk)


# Continue with your processing

print ("\nThe roman numeral", romanval, "is equal to", decval)

The reason that the if is false can be found in the following code

a = 3
b = ( a == int)
c = type (a)
d = type(int)
print a, b, c, d

OUTPUT: 3, False, (type 'int') (type 'type')

This is because you are attempting to test the values. If you really wanted to test this as shown, it should be

type(a) == int:

However, in your code type(numberchk) would return "str" because you have not yet converted it. That is why you must use the try: except: method.

sabbahillel
  • 4,357
  • 1
  • 19
  • 36