我的目标是让用户在I和V之间输入两个罗马数字并将它们加在一起。我想同时以十进制值和罗马数字等价物显示方程。
但是,当我输入I和V以外的值时,该值将被分配为'NoneType',并且程序将失败。我试过将返回值设置为str(),然后再调用int(),将罗马num设置为'':num_value =#,然后在最后返回num_value,但没有一个起作用。
我认为我缺少了一些非常基本的东西,这与区分具有重复字符的值有关,例如“ I”(可以)和“ II”,后者被分配了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))
使用I和V以外的任何其他值时
Traceback (most recent call last):
File ... line 26, in <module>
totalNumber = firstNumber + secondNumber
TypeError: unsupported operand type(s) for +: 'NoneType' and 'NoneType'''
答案 0 :(得分:1)
get_user_num(roman_num)
函数存在一些问题,如前所述,您需要使用“ ==”而不是is。
https://stackoverflow.com/a/15008404/8426559
您也没有故障保险,因此,如果用户输入非法内容,则不会返回任何内容,这将破坏您的代码。快速解决方案可以在函数末尾添加行return get_user_num(input("please enter a roman numeral between I and V: "))
:
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: "))
您也可以使用if
代替elif
,因为如果满足任何条件,该函数将返回。不过,如果您打算扩展到10以上,我建议使用一种更健壮的算法来提供一种更清洁的方法。
答案 1 :(得分:0)
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))