Even if the first command in if is true it doesn't print what i want , it only print the else command

前端 未结 4 1525
时光取名叫无心
时光取名叫无心 2021-01-24 14:22
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\",         


        
4条回答
  •  执念已碎
    2021-01-24 14:43

    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.

提交回复
热议问题