我正在制作一個羅馬數字到整數的轉換器。在下面的代碼中,您將看到函式math_logic。當我給出 inputCCC時,程式應該跳過if陳述句和elif陳述句(因為關鍵字and)并直接進入else陳述句,因為只有兩個條件之一滿足。該else陳述句應使用引數char_0作為鍵回傳字典值。但是,程式將運行第二elif條陳述句中的代碼并TypeError: unsupported operand type(s) for : 'int' and 'NoneType'作為錯誤回傳。
我不確定為什么會這樣。您可以在第 38 行放置一個除錯點并分步執行 4 次以解決我遇到的問題。
請看下面的代碼
romanToIntDictionary = {
"I": 1,
"V": 5,
"X": 10,
"L": 50,
"C": 100,
"D": 500,
"M": 1000
}
subtraction_happened = [
4,
9,
40,
90,
400,
900
]
def convert_roman_to_int(string, checker_state):
characters_left = len(string)
character_list = []
total = 0
if checker_state:
print("What you entered is a valid roman numeral.")
for character in string:
character_list.append(character)
while characters_left > 1:
did_i_sub = False
for item in subtraction_happened:
if (not did_i_sub) and (item == math_logic(character_list[0], character_list[1], romanToIntDictionary)): #the order of multiple conditions matters
total = total math_logic(character_list[0], character_list[1], romanToIntDictionary)
characters_left -= 2
character_list.pop(0)
character_list.pop(0)
did_i_sub = True
if not did_i_sub:
total = total math_logic(character_list[0], character_list[1], romanToIntDictionary)
characters_left -= 1
character_list.pop(0)
while characters_left == 1:
total = total romanToIntDictionary[character_list[0]]
characters_left -= 1
character_list.pop(0)
print(total)
if not checker_state:
print("What you entered is not a roman numeral.")
def math_logic(char_0, char_1, r_to_i_dict):
if (char_0 == "I") and (char_1 == "V" or "X"):
if char_1 == "V":
return 4
elif char_1 == "X":
return 9
elif (char_1 == "L" or "C") and (char_0 == "X"):
if char_1 == "L":
return 40
elif char_1 == "C":
return 90
elif (char_1 == "D" or "M") and (char_0 == "C"):
if char_1 == "D":
return 400
elif char_1 == "M":
return 900
else:
return r_to_i_dict[char_0]
def roman_numeral_checker(string):
is_roman_numeral = True
characters_left = len(string)
while is_roman_numeral and characters_left > 0:
for character in string:
if character not in romanToIntDictionary.keys():
is_roman_numeral = False
characters_left -= 1
if not is_roman_numeral:
return False
if is_roman_numeral:
return True
string_from_user = (input("Enter a roman numeral to convert: ")).upper()
convert_roman_to_int(string_from_user, roman_numeral_checker(string_from_user))
uj5u.com熱心網友回復:
問題在于您的布爾邏輯:
value = 'c'
print(value == 'X' or 'V')
'V'
這是因為這'V'是一個“真實”的價值:
bool('V')
True
所以你說if value == 'X' or True:的是永遠是True。因此,有一個else沒有評估:
if value == 'X' or 'V':
if value == 'X':
print('X!')
elif value == 'V':
print('V!')
else:
print('unexpected')
unexpected
正確的語法是:
if value == 'X' or value == 'V':
或者更簡潔:
if value in ('X', 'V'):
if value == 'X':
do something
else:
do something
else 確保所有情況都被覆寫,而且它們是,因為value只能是“X”或“V”。
所以你的整個數學邏輯函式將是:
def math_logic(char_0, char_1, r_to_i_dict):
if char_0 == "I" and char_1 in ('V', 'X'):
if char_1 == "V":
return 4
else:
return 9
elif char_1 in ('L', 'C') and char_0 == "X":
if char_1 == "L":
return 40
else:
return 90
elif char_1 in ('D', 'M') and char_0 == "C":
if char_1 == "D":
return 400
else:
return 900
else:
return r_to_i_dict[char_0]
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/517954.html
