0

My goal is to get the user to input two roman numerals between I and V and to add them together. I want to display the equation in both decimal values and their roman numeral equivalents.

However, when ever I put values other than I and V, the value gets assigned 'NoneType', and program fails. I've tried setting the returns to str() and then calling int() later, setting roman num is '': num_value = # and then returning num_value at the end, but none of it has worked.

I think I'm missing something very fundamental, and it has to do with differentiating between values with repeating characters, such as 'I' (which is ok) and 'II' which gets assigned NoneType.

firstNumeral = input("please enter a roman numeral between I and V: ")
secondNumeral = input('please enter a second roman numeral between I and V: ')
def get_user_num(roman_num):
    if roman_num is 'I':
        return int(1)
    elif roman_num is 'II':
        return int(2)
    elif roman_num is 'III':
        return int(3)
    elif roman_num is 'IV':
        return int(4)
    elif roman_num is 'V':
        return int(5)
    elif roman_num is 'VI':
        return int(6)
    elif roman_num is 'VII':
        return int(7)
    elif roman_num is 'VIII':
        return int(8)
    elif roman_num is 'IX':
        return int(9)
    elif roman_num is 'X':
        return int(10)
firstNumber = get_user_num(firstNumeral)
secondNumber = get_user_num(secondNumeral)
totalNumber = firstNumber + secondNumber
def get_roman_number(result_number):
    if result_number == 1:
        return "I"
    elif result_number == 2:
        return "II"
    elif result_number == 3:
        return 'III'
    elif result_number == 4:
        return 'IV'
    elif result_number == 5:
        return 'V'
    elif result_number == 6:
        return 'VI'
    elif result_number == 7:
        return 'VII'
    elif result_number == 8:
        return 'VIII'
    elif result_number == 9:
        return 'IX'
    elif result_number == 10:
        return 'X'
totalNumeral = get_roman_number(totalNumber)
print('Decimal values {} + {} = {}'.format(firstNumber,secondNumber, totalNumber))
print('Roman Numeral values {} + {} = {}'.format(firstNumeral, secondNumeral, totalNumeral))

when using any values other than I and V i get

Traceback (most recent call last):
  File ... line 26, in <module>
    totalNumber = firstNumber + secondNumber
TypeError: unsupported operand type(s) for +: 'NoneType' and 'NoneType'''
iTheSailor
  • 13
  • 2

2 Answers2

1

firstNumeral = input("please enter a roman numeral between I and V: ")
secondNumeral = input('please enter a second roman numeral between I and V: ')
def get_user_num(roman_num):
    if roman_num is 'I':
        return int(1)
    elif roman_num == 'II':
        return int(2)
    elif roman_num == 'III':
        return int(3)
    elif roman_num == 'IV':
        return int(4)
    elif roman_num == 'V':
        return int(5)
    elif roman_num is 'VI':
        return int(6)
    elif roman_num == 'VII':
        return int(7)
    elif roman_num == 'VIII':
        return int(8)
    elif roman_num == 'IX':
        return int(9)
    elif roman_num == 'X':
        return int(10)
firstNumber = get_user_num(firstNumeral)
secondNumber = get_user_num(secondNumeral)
print(secondNumber)
totalNumber = firstNumber + secondNumber
def get_roman_number(result_number):
    if result_number == 1:
        return "I"
    elif result_number == 2:
        return "II"
    elif result_number == 3:
        return 'III'
    elif result_number == 4:
        return 'IV'
    elif result_number == 5:
        return 'V'
    elif result_number == 6:
        return 'VI'
    elif result_number == 7:
        return 'VII'
    elif result_number == 8:
        return 'VIII'
    elif result_number == 9:
        return 'IX'
    elif result_number == 10:
        return 'X'
totalNumeral = get_roman_number(totalNumber)
print('Decimal values {} + {} = {}'.format(firstNumber,secondNumber, totalNumber))
print('Roman Numeral values {} + {} = {}'.format(firstNumeral, secondNumeral, totalNumeral))
use '==' to check condition enter image description here
Sebin Sunny
  • 1,753
  • 10
  • 9
1

There are a couple things wrong with your get_user_num(roman_num) function, as already mentioned you need to use "==" instead of is. https://stackoverflow.com/a/15008404/8426559

Also you have no fail-safes, so None will be returned if user types something illegal, which will break your code. A quick fix for this could be adding the line return get_user_num(input("please enter a roman numeral between I and V: ")) at the end of the function:

def get_user_num(roman_num):
    if roman_num == 'I':
        return int(1)
    if roman_num == 'II':
        return int(2)
    if roman_num == 'III':
        return int(3)
    if roman_num == 'IV':
        return int(4)
    if roman_num == 'V':
        return int(5)
    if roman_num == 'VI':
        return int(6)
    if roman_num == 'VII':
        return int(7)
    if roman_num == 'VIII':
        return int(8)
    if roman_num == 'IX':
        return int(9)
    if roman_num == 'X':
        return int(10)
    return get_user_num(input("please enter a roman numeral between I and V: "))

You can also use if instead of elif because the function will return if any condition is met. Though, I'd suggest a cleaner approach with a more robust algorithm for this if you plan on scaling up beyond 10.

Pete
  • 121
  • 3