Browse code

Added Roman numeral program

haywalk authored on 20/01/2020 02:55:54
Showing 2 changed files

... ...
@@ -134,6 +134,16 @@ Recursive Guessing Game (24 August)
134 134
 
135 135
     The user chooses a number between 0 and 100 (unknown to the computer), and the computer will guess it. A light project that I included because of recursion. 
136 136
 ------------------------------------------------------------------------------------------------------------
137
+Roman Numerals to Integer (19 January 2020)
138
+    *Dictionary data type
139
+    *Conditional
140
+    *For loop
141
+        *Continue
142
+    *Lists/using indexed
143
+    *Iteration
144
+
145
+    The user inputs a number in Roman numerals and it is returned as an integer.
146
+------------------------------------------------------------------------------------------------------------
137 147
 Monty Python's Python Soundboard (31 July)
138 148
     *Tkinter GUI Library
139 149
         *Buttons
140 150
new file mode 100644
... ...
@@ -0,0 +1,29 @@
1
+'''
2
+Roman numeral to integer converter
3
+Hayden Walker, 19 January 2020
4
+'''
5
+romanNum = input().upper()
6
+parts = list()
7
+num = 0
8
+
9
+romanLets = {"I":1, "V":5, "X":10, "L":50, "C":100, "D":500, "M":1000 }
10
+
11
+for let in romanNum:
12
+	'''
13
+	Replace the letters with the values they represent
14
+	'''
15
+	parts.append(romanLets[let])
16
+
17
+for part in parts:
18
+	ind = parts.index(part)
19
+	
20
+	# If not at the end & number is greater than next index, subtract it
21
+	if ind != len(parts) - 1:
22
+		if part < parts[ind + 1]:
23
+			num -= part
24
+			continue
25
+
26
+	# Otherwise add the number
27
+	num += part
28
+
29
+print(num)