forked from LKnopf/Python_precourse
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathassignment_2.py
More file actions
60 lines (44 loc) · 2.34 KB
/
assignment_2.py
File metadata and controls
60 lines (44 loc) · 2.34 KB
1
def translate_numeral(number): if type(number) == str: # check type of input roman_numeral = number #======enter code below====== c_map= {'I' : 1, 'V' : 5, 'X' : 10, 'L' : 50, 'C' : 100,'D' : 500, 'M' : 1000}#c_map gives a dictionary that assigns each roman characters recpective value arabic_number = 0 #initial value for arabic number before we start calculating a = list(roman_numeral) #makes a list of string characters a.reverse() #reverses the order of the list #Because my algorithm wants to read the last character first print(type(a)) #I want to see type the list which will go into the for loop print(a) #I want to see the list which will go into the for loop for rom in range(len(a)): #rom abbreviation for roman character print(arabic_number) #prints me out each step of the loop so that I could check where things go wrong#in my algortihm (when they did go wrong) could be deleted after if rom == 0: #I assign the value of the initial character to arabic_number arabic_number += c_map[a[rom]] #now for the next characters I can compare the value with the next one # elif rom >0 and c_map[a[rom - 1]] > c_map[a[rom]]: arabic_number += -1 * c_map[a[rom]] elif rom > 0 and c_map[a[rom -1]] <= c_map[a[rom]]: arabic_number += c_map[a[rom]] else: print('Input not valid.') print('{0} translated to {1}!'.format(roman_numeral, arabic_number)) # print the result return arabic_number ## The function works well, ## But I still get Not all numerals translated correctly result. ¯\_(ツ)_/¯ ## I Thought it might be because I reversed the roman characters ## for my algorithm But I am not sure if __name__ == '__main__': input_numerals = ['X', 'XXVIII', 'LXXI', 'XCIX', 'MCMXCIV'] outputs = [10, 28, 71, 99, 1994] results = [True, True, True, True, True] for index in range(5): if translate_numeral(input_numerals[index]) != outputs[index]: results[index] = False if False in results: print('Not all numerals translated correctly. Try again.') else: print('Well done! All numerals translated correctly.')