# Thread: [Beginner] Programming Challenge: 5

1. ## Re: [Beginner] Programming Challenge: 5

Originally Posted by CoyleTrydan
Here is my submission for Challenge 5. I have the same question here as Challenge 4 (which I just posted as well) - regarding my usage of functions, and if that's proper form or not. I like how it breaks things up into segments, but I'm very new to Python and not sure yet if I'm doing something silly.
Splitting code up into self contained segments with a clear task and clear inputs and results is exactly what functions are designed to do - so I would say you are using them in exactly the right way.

2. ## Re: [Beginner] Programming Challenge: 5

This function should be named something like isCorrect and return True or False. Also, return is not a function, no parentheses are needed.

Code:
```def checkGuess(guess, target):
if guess == target:
print("That's correct! You win!")
return(1)
elif guess < target:
print("That guess is too low.")
return(0)
elif guess > target:
print("That guess is too high.")
return(0)```

3. ## Re: [Beginner] Programming Challenge: 5

Code:
```while guess != num:
try:
guess = int(input("Guess: "))

if guess > num and guess < 101:
print("Too high, retry.")
tries += 1
elif guess < num and guess > 0:
print("Too low, retry.")
tries += 1
elif guess < 1 or guess > 100:
print("Don't be stupid, you have to type an integer between 1 and 100!")

except:
print("Are you crazy? Type an integer!")```
"Catch all" except's are very bad. Suppose any other exception is thrown, for example the user presses Ctrl+C, you will still get "Type an integer". Also, try/except should only cover what is relevant. If an exception is thrown by anything after the first line, you will get "type an integer" also. This is correct:

Code:
```while guess != num:
try:
guess = int(input("Guess: "))
except ValueError:
print("Are you crazy? Type an integer!")
continue

if guess > num and guess < 101:
print("Too high, retry.")
tries += 1
elif guess < num and guess > 0:
print("Too low, retry.")
tries += 1
elif guess < 1 or guess > 100:
print("Don't be stupid, you have to type an integer between 1 and 100!")```

4. First Cup of Ubuntu
Join Date
Feb 2013
Beans
3

## Re: [Beginner] Programming Challenge: 5

Ok, thank you Bachstelze for the feedback.

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•