1
numberchk=(int(input("Enter a Roman numeral or a Decimal numeral:" )))
def int2roman(number):
    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 number >= value:
            result += numeral
            number -= value
    return result
if numberchk==int:
    print(int2roman(int(numberchk)))

else:
    print("error")
Ashwini Chaudhary
  • 244,495
  • 58
  • 464
  • 504

4 Answers4

1

Use isinstance(numberchk, int) instead, because int is a type but numberchk is an instance of that type.

Since int(input(... always returns an integer as long as it can, you don't have to check it using if-else. To suppress error raising if input is not an integer, use try-except as @poke mentioned.

You can also use a while-loop and break to request the user input repeatedly until you get an legal input:

while True:
    try:
        numberchk=int(input("Enter a Roman numeral or a Decimal numeral:" ))
        break
    except ValueError:
        print('error')
print(int2roman(numberchk))
zhangxaochen
  • 32,744
  • 15
  • 77
  • 108
0
if numberchk==int:

This will check if numberchk is equal to the int type. It will not check if numberchk is an integer (which you probably want to do). The correct way to check its type would be this:

if isinstance(numberchk, int):

However, this won’t make sense either. The way you get numberchk is by calling int() on a string:

numberchk=int(input(…))

So numberchk will always be an int. However, calling int() on a string that is not a number can fail, so you probably want to catch that error to find out whether or not the input was a number:

try:
    numberchk = int(input("Enter a Roman numeral or a Decimal numeral:"))
except ValueError:
    print('Entered value was not a number')

But this will again will be problematic, as—at least judging by the message you’re printing—you also want to accept Roman numerals, which can’t be converted to integers by int. So you should also write a function that takes a Roman numeral and converts it into an int.

poke
  • 369,085
  • 72
  • 557
  • 602
-1

Check the integer type instead of matching variable with int.

You can check type of variable with isinstance method.

Nilesh
  • 20,521
  • 16
  • 92
  • 148
-1

Try using isdigit() function.

replace this part on your code

if numberchk==int:

with

if numberchk.isdigit():
Ashwini Chaudhary
  • 244,495
  • 58
  • 464
  • 504
zlogic
  • 69
  • 3