Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit b26ec97

Browse files
Added error handling and comments
1 parent 1049ea2 commit b26ec97

File tree

3 files changed

+46
-20
lines changed

3 files changed

+46
-20
lines changed

‎05-GuessingGame.py

Lines changed: 18 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -8,23 +8,30 @@
88
secretNum = randint(0,50)
99
# Generate random int adapted from https://stackoverflow.com/questions/3996904/generate-random-integers-between-0-and-9
1010

11-
# Take input from the user and convert to int
12-
userGuess = int(input("Your guess: "))
13-
1411
alreadyGuessed = []
12+
userGuess = 0
1513

1614
# While guess is wrong, take more inputs
1715
while userGuess != secretNum:
18-
if userGuess < secretNum: # If guess is too low, tell the user & ask for another input
19-
print("Too low, try again.")
20-
userGuess = int(input())
21-
alreadyGuessed.append(userGuess) # Add the guess to an array
2216

23-
elif userGuess > secretNum: # If guess is too high, tell user & ask again
24-
print("Too high, try again.")
25-
userGuess = int(input())
26-
alreadyGuessed.append(userGuess) # Add guess to array
17+
# try except ValueError adapted from Python 3 docs -
18+
# https://docs.python.org/3.3/tutorial/errors.html#handling-exceptions
19+
try:
20+
# Take input from the user and convert to int
21+
userGuess = int(input("Your guess: "))
22+
23+
if userGuess < secretNum: # If guess is too low, tell the user & ask for another input
24+
print("Too low, try again.")
25+
#userGuess = int(input())
26+
alreadyGuessed.append(userGuess) # Add the guess to an array
2727

28+
elif userGuess > secretNum: # If guess is too high, tell user & ask again
29+
print("Too high, try again.")
30+
#userGuess = int(input())
31+
alreadyGuessed.append(userGuess) # Add guess to array
32+
except ValueError:
33+
print("Please enter only valid whole numbers.")
34+
2835
# If guess is right, tell user
2936
if userGuess == secretNum:
3037
print("You're right!")

‎06-LargestAndSmallest.py

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,19 @@
33
# Write a function that returns the largest and smallest elements in a list.
44

55
numbers = []
6+
i = 0
67

78
print("Please enter 5 numbers.")
89

9-
for i in range (0,5):
10-
listItem = float(input(str(i + 1) + ": ")) # Get input from user
11-
numbers.append(listItem) # Add item to array of numbers
10+
while i < 5:
11+
try:
12+
listItem = float(input(str(i + 1) + ": ")) # Get input from user
13+
numbers.append(listItem) # Add item to array of numbers
14+
i = i + 1
15+
16+
except ValueError:
17+
print("Please only enter number values, try again.")
18+
1219

1320
print(numbers) # Output the list
1421

‎08-MergeSort.py

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,18 +5,30 @@
55
list1 = []
66
list2 = []
77
newlist = []
8+
i = 0
89

910
print("Please enter 3 numbers for each list.")
1011

1112
print("List 1")
12-
for i in range (0,3):
13-
listItem = float(input(str(i + 1) + ": "))
14-
list1.append(listItem)
13+
while i < 3:
14+
try:
15+
listItem = float(input(str(i + 1) + ": "))
16+
list1.append(listItem)
17+
i = i + 1
18+
19+
except ValueError:
20+
print("Please only enter number values, try again.")
1521

1622
print("List 2")
17-
for i in range (0,3):
18-
listItem = float(input(str(i + 1) + ": "))
19-
list2.append(listItem)
23+
i = 0 # Reset i
24+
while i < 3:
25+
try:
26+
listItem = float(input(str(i + 1) + ": "))
27+
list1.append(listItem)
28+
i = i + 1
29+
30+
except ValueError:
31+
print("Please only enter number values, try again.")
2032

2133
newList = list1 + list2
2234

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /