Check if an input is a valid roman numeral

后端 未结 5 607
醉话见心
醉话见心 2021-01-14 10:46

I got a program that converts Roman numerals to integers and vice versa. My problem is that I don´t really know how to create a function that checks if the user input is a v

5条回答
  •  小鲜肉
    小鲜肉 (楼主)
    2021-01-14 11:18

    def checkIfRomanNumeral(numeral):
    """Controls that the userinput only contains valid roman numerals"""
        numeral = numeral.upper()
        validRomanNumerals = ["M", "D", "C", "L", "X", "V", "I", "(", ")"]
        valid = True
        for letters in numeral:
            if letters not in validRomanNumerals:
                print("Sorry that is not a valid roman numeral")
                valid = False
                break
        return valid
    

    Returns a boolean whether the given 'numeral' is roman numeral or not.

提交回复
热议问题