簡體   English   中英

Python 3.5.2羅馬數字測驗

[英]Python 3.5.2 Roman Numeral Quiz

因此,我正在為學校做這項作業。 我必須創建一個類似游戲的測驗,它將提示用戶將羅馬數字加在一起並輸入他們的答案。 然后它將用正確的答案檢查用戶的答案,並告訴用戶是對還是錯。

到目前為止,我有這個:

class RomanNumeral:
    index = 0
    while index < len(integer_list) - 1:
        #if a lower number is first, it should be subtracted, like IX, 
        #the one should be subtracted, but the 10 added
        if integer_list[index] < integer_list[index + 1]:
            r_integer -= integer_list[index]
        else:
            r_integer += integer_list[index]
        index += 1
    #Always add the last number
    r_integer += integer_list[index]
    #Store r_integer as an instance data item
    self.__integer = r_integer
    return

def main():
        roman1 = RomanNumeral('random')
        roman2 = RomanNumeral('random')
        correct_answer = roman1 + roman2
main()

但是當我運行它時,出現此錯誤:

r_integer += integer_list[index]
UnboundLocalError: local variable 'r_integer' referenced before assignment

有關如何解決此問題的任何建議?

同樣,我需要幫助int方法重載以將羅馬數字更改為整數,以便可以將它們加在一起。

你的錯誤說

local variable 'r_integer' referenced before assignment

這意味着您嘗試在定義變量之前使用它。 while循環之前將r_integer定義為0(或其他一些數字),然后應解決您的問題。

您需要在while循環之前初始化r_integer 在#####注釋下方添加到您的代碼中

    #Add if the number is greater than the one that follows, otherwise
    #subtract r_integer = 0
    #Stands for roman integer or the integer equivalent of the roman string
    index = 0
    ##### Initialize r_integer before the while loop
    r_integer = 0 
    while index < len(integer_list) - 1:
        #if a lower number is first, it should be subtracted, like IX, 
        #the one should be subtracted, but the 10 added
        if integer_list[index] < integer_list[index + 1]:
            r_integer -= integer_list[index]
        else:
            r_integer += integer_list[index]
        index += 1
    #Always add the last number
    r_integer += integer_list[index]

您沒有使用為self定義的整數。 嘗試在之后添加聲明

 r_string = r_string.upper()

r_integer = self.__integer

這樣,您就可以使用本地副本。

然而,你需要重載它的回答是整數方法這個帖子

這是我的解決方案:

class Solution:
def romanToInt(self, s):
    """
    :type s: str
    :rtype: int
    """
    array_representation = []
    for i in range(len(s)):
        array_representation.append(s[i]);
    total = 0
    i = 0
    print(array_representation)
    while (i < len(array_representation)):

        if (i < len(array_representation) - 1 and val(array_representation[i + 1]) > val(array_representation[i])):
            total += (val(array_representation[i + 1]) - val(array_representation[i]))
            i = i + 2;
        else:
            total += val(array_representation[i]);
            i = i + 1;

    return total;

這是我使用的輔助功能

def val(value):
if (value == "I"):
    return 1
if (value == "V"):
    return 5
if (value == "X"):
    return 10;
if (value == "L"):
    return 50;
if (value == "C"):
    return 100;
if (value == "D"):
    return 500;
if (value == "M"):
    return 1000;

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM