1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

Python homework went ok, but...

Discussion in 'Scripting & Programming' started by tripwire45, Mar 17, 2008.

  1. tripwire45
    Honorary Member

    tripwire45 Zettabyte Poster

    13,493
    179
    287
    I managed to get my most recent Python homework assignment done, but I can't believe it's in the most efficent manner possible. Frankly, an awful lot of accomplishing an assignment is trial and error plus going back over previous examples in the text and patching together bits and pieces of what was previously presented in order to solve the homework problem.

    The thing is, while I managed to solve this problem, I have to believe there is a better way to write the code to accomplish the task.

    Incidently, the task is to write a program that makes the user guess my name, but the user only has three chances. If they don't guess my name after the third time, the program exits. Here's what I did:
    Code:
    name = "foobar"
    count = 0
    while name != "Jim":
        name = raw_input("What is my name?")
        count = count + 1
        if name == "Jim":
            print "You guessed my name."
        elif count == 3:
    	print "You lose."
    	break
    How did I do and how could I have done it better?
     
    Certifications: A+ and Network+
  2. ThomasMc

    ThomasMc Gigabyte Poster

    1,507
    49
    111
    shouldn't you be using some sort of to lower for the input? and have your condition "jim"
     
    Certifications: MCDST|FtOCC
    WIP: MCSA(70-270|70-290|70-291)
  3. hbroomhall

    hbroomhall Petabyte Poster Gold Member

    6,623
    115
    224
    This is what learning programming is all about. There are many ways of solving a problem usualy, but to find the 'most efficient' takes experience and practise. If you get a solution, be glad. Next time you will probably find a better one.

    I find that reading books on such problems, and looking at other code helps a lot.

    For those who get serious about it - there are books like 'Design Patterns' to take you further.

    Again - I don't know Python, but that looks fairly good and succinct. My first thought is that you could lose the first line. The first time you reference a variable it springs into life, but with no contents. So the name != "Jim" will be true anyway.

    Harry.
     
    Certifications: ECDL A+ Network+ i-Net+
    WIP: Server+
  4. hbroomhall

    hbroomhall Petabyte Poster Gold Member

    6,623
    115
    224
    And now I've tried it I see my error - I have too much Perl in my thinking!

    But name = "" for a first line is fine.

    Harry.
     
    Certifications: ECDL A+ Network+ i-Net+
    WIP: Server+
  5. Mathematix

    Mathematix Megabyte Poster

    969
    35
    74
    If you want to go with the most efficient way you could:

    1. Lose the count variable. You don't need to explicitly count the number of times the user guesses your name.

    2. You don't need the variable 'name'.

    I'll leave you to stew over it for a while. ;)
     
    Certifications: BSc(Hons) Comp Sci, BCS Award of Merit
    WIP: Not doing certs. Computer geek.
  6. Mathematix

    Mathematix Megabyte Poster

    969
    35
    74
    Good spot! :biggrin
     
    Certifications: BSc(Hons) Comp Sci, BCS Award of Merit
    WIP: Not doing certs. Computer geek.
  7. tripwire45
    Honorary Member

    tripwire45 Zettabyte Poster

    13,493
    179
    287
    I know it isn't much of a change, but is this any better?
    Code:
    count = 0
    while True:
        name = raw_input("What is my name?")
        count += 1
        if name == "Jim":
            print "You guessed my name."
            break
        elif count == 3:
            print "You lose."
            break
    EDIT: Not sure how I'd get this to work without the count variable. :oops:
     
    Certifications: A+ and Network+
  8. hbroomhall

    hbroomhall Petabyte Poster Gold Member

    6,623
    115
    224
    Actualy - I was going to comment this morning along those lines.

    The problem as it stands is that you want to get input *at least once*. This is a classic type of algorithm that is usualy solved by a "do ... while..." construct.

    With a "while" loop the test is at the start of the block. With a "do... while" loop the test is done at the end of the block.

    The snag is that python doesn't have "do... while". This seems to be much commented on, and stems partly from the indentation style of block demarking, where it is difficult to come up with a method of implementing it within that style.

    All the python experts seem to agree that the best current way of dealing with "do... while" is the way you have done it - with a "while True:" block.

    To eliminate the 'name' variable try something along the lines of
    Code:
     if "jim" = raw_input()"
    Given the original conditions of the task, I don't immediately see how to eliminate the 'count' variable.

    Harry.
     
    Certifications: ECDL A+ Network+ i-Net+
    WIP: Server+
  9. Mathematix

    Mathematix Megabyte Poster

    969
    35
    74
    You are so close! The only loop construct the you forgot to mention are 'for loops'. My wording was explicitly lose the 'count' variable because you do need some way to count the number of guesses a user makes, but are not required to declare a separate variable to store this count. Also check out 'range'. :biggrin
     
    Certifications: BSc(Hons) Comp Sci, BCS Award of Merit
    WIP: Not doing certs. Computer geek.
  10. tripwire45
    Honorary Member

    tripwire45 Zettabyte Poster

    13,493
    179
    287
    Since the sections I was working on and that contain this exercise involve "for" loops (including ranges) and boolean expressions, I don't doubt that the solution you suggest is the intended one. It's still way too early in my part of the world and I need more coffee before my brain starts to wake up. This one feels like it's on the "tip of my brain", but I can't quite articulate the solution (so near and yet so far...).
     
    Certifications: A+ and Network+
  11. Mathematix

    Mathematix Megabyte Poster

    969
    35
    74
    No probs. Let me know if you need any more clues or post the solution.

    I would suggest that you look into examples of for-loops and ranges to get a better feel for them if you haven't already. :)
     
    Certifications: BSc(Hons) Comp Sci, BCS Award of Merit
    WIP: Not doing certs. Computer geek.
  12. tripwire45
    Honorary Member

    tripwire45 Zettabyte Poster

    13,493
    179
    287
    Ok...I cheated. I didn't solve this myself. I've been poking around the Internet and asking elsewhere. What I discovered though, is printed below and seems to fit the requirements. What I need to know is *why* it works. What is it about the statement:
    Code:
    for i in range(3):
    that gives the user three chances by using "range(3)" (I know that the letter "i" can actually be a different letter since I tried changing it)?
    Here's the solution:
    Code:
    success = False
    for i in range(3):
        if raw_input("What is my name?") == "Jim":
            success = True
            break
    if success:
        print "You guessed my name."
    else:
        print "You lose."
     
    Certifications: A+ and Network+
  13. ThomasMc

    ThomasMc Gigabyte Poster

    1,507
    49
    111
    :rolleyes: CaSe jim does not = Jim

    does this work?

    Code:
    if raw_input("What is my name?").lower() == "jim":
    
     
    Certifications: MCDST|FtOCC
    WIP: MCSA(70-270|70-290|70-291)
  14. Mathematix

    Mathematix Megabyte Poster

    969
    35
    74
    Yup, and exactly part of what I would have written. :biggrin
     
    Certifications: BSc(Hons) Comp Sci, BCS Award of Merit
    WIP: Not doing certs. Computer geek.
  15. Mathematix

    Mathematix Megabyte Poster

    969
    35
    74
    Bingo! :D

    Normally when you want to see how something works in Python you type it directly into the Python interpreter.

    Summon the DOS commant prompt and type "python'. When you get this symbol '>>>' type
    Code:
    for i in range(3):
        print i
    
    Then press return twice to see the result.
     
    Certifications: BSc(Hons) Comp Sci, BCS Award of Merit
    WIP: Not doing certs. Computer geek.
  16. tripwire45
    Honorary Member

    tripwire45 Zettabyte Poster

    13,493
    179
    287
    Like this?
    Code:
    jmpyles@lamp:~/python$ python
    Python 2.4.3 (#2, Oct  6 2006, 07:52:30)
    [GCC 4.0.3 (Ubuntu 4.0.3-1ubuntu5)] on linux2
    Type "help", "copyright", "credits" or "license" for more information.
    >>> for i in range(3):
    ...      print i
    ...
    0
    1
    2
    >>>
     
    Certifications: A+ and Network+
  17. Mathematix

    Mathematix Megabyte Poster

    969
    35
    74
    Yup. :D
     
    Certifications: BSc(Hons) Comp Sci, BCS Award of Merit
    WIP: Not doing certs. Computer geek.

Share This Page

Loading...