Page 15 of 15 FirstFirst ... 5131415
Results 141 to 144 of 144

Thread: [Beginner] Programming Challenge: 5

  1. #141
    Join Date
    May 2008
    Location
    UK
    Beans
    1,451
    Distro
    Ubuntu 8.04 Hardy Heron

    Re: [Beginner] Programming Challenge: 5

    Quote Originally Posted by CoyleTrydan View Post
    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.
    Tony - Happy to try to help.
    Unless otherwise stated - all code posted by me is untested. Remember to Mark the Thread as Solved.
    Ubuntu user number # 24044 Projects : TimeWarp - on the fly Backups

  2. #142
    Join Date
    Nov 2005
    Location
    Sendai, Japan
    Beans
    11,296
    Distro
    Kubuntu

    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. #143
    Join Date
    Nov 2005
    Location
    Sendai, Japan
    Beans
    11,296
    Distro
    Kubuntu

    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. #144
    Join Date
    Feb 2013
    Beans
    3

    Re: [Beginner] Programming Challenge: 5

    Ok, thank you Bachstelze for the feedback.

Page 15 of 15 FirstFirst ... 5131415

Bookmarks

Posting Permissions

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