Skip to content

Commit 035756f

Browse files
committed
Add solution to new exercise
1 parent bd06bff commit 035756f

File tree

1 file changed

+17
-3
lines changed

1 file changed

+17
-3
lines changed

Diff for: ch08-conditional-logic/6-recover-from-errors.py

+17-3
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,27 @@
22
# Solution to review exercises
33

44

5+
# Exercise 1
56
# Ask the user to enter an integer.
67
# Repeat the process if the user hasn't entered an integer.
7-
repeat = True
8-
while repeat:
8+
while True:
99
try:
1010
my_input = input("Type an integer: ")
1111
print(int(my_input))
12-
repeat = False
12+
break
1313
except ValueError:
1414
print("try again")
15+
16+
17+
# Exercise 2
18+
# Print character and specifid index in string
19+
20+
input_string = input("Enter a string: ")
21+
22+
try:
23+
index = int(input("Enter an integer: "))
24+
print(input_string[index])
25+
except ValueError:
26+
print("Invalid number")
27+
except IndexError:
28+
print("Index is out of bounds")

0 commit comments

Comments
 (0)