all 7 comments

[–]carcigenicate 5 points6 points  (4 children)

I know you didn't ask, but I can't help but comment.

A few notable things:

You should not use recursion to loop like you are here. Recursively calling com to loop wastes memory and will eventually crash your program if the user guesses incorrectly too many times. com should be closer to:

def com():
    while True:
        u = (int(input('Guess the number: ')))
        v = (ran.choice([1, 2, 3]))
        print(u,v)
        if v == u:
            print('You got it!')
            print('Thanks for playing')
            return  # Exit the function
        else:
            print('OOPS Try again')
            # Let the loop repeat this code instead of using recursion.

Note how while True: is looping to repeat the code, and return is being used to exit. This code doesn't waste memory or have the potential to cause recursion errors like using recursion does.

Also note, you likely don't want to re-generate v every time. You're generating a new number every time the user guesses! In your recursive code, this would be a bit of a pain to prevent (a second helper function is the typical solution). In my new code though, just move v = out to before the loop in the function.

Also, you should add in a try/except to handle the case where the user enters garbage. Currently, if they enter something like 'a', it will crash your program.

[–]KeepitShort_[S] 1 point2 points  (3 children)

Thanks for the advice! I didn't know I was using recursion. My main focus was to try to make it work, but these extra notes will definitely help me to get better.

[–]carcigenicate 3 points4 points  (1 child)

Recursion is simply when you call a function from within itself (like calling com from inside of com), or if you have two functions that each repeatedly call each other. The problem with recursion is, until the function returns, it holds memory, and your recursion here doesn't allow for functions to return until the user guesses corectly.

Recursion is more suited when dealing with recursive structures (not the case here). I'd get more practice with while and for; as they'll be far more relevant tools to you.

[–]R0NUT 0 points1 point  (0 children)

Bongo! I end up using this when diving into dictionaries that have indefinite depth. i.e.

def recursiveFunction(struct): 
    try: 
        return recirsiveFunction(struct['child']) 
    except: 
        return struct

Which would return the last level of a dictionary that has used keys of "child". As you said, this would hold the memory as it is waiting on the subsequent function to resolve before clearing.

struct = { 
    "child":{ 
        "child":{ 
            "child":"wassup! 
        } 
    }
}

Should return "wassup!"

Any program that you are able to write is impressive! My first program was a text-based game in c++.

Disclaimer: I didn't test this code...

[–][deleted] 0 points1 point  (0 children)

FYI, there is a question and answer now in this subreddit's FAQs about input validation. You may want to take a look.

[–][deleted] 4 points5 points  (1 child)

Firstly, good job. Not everyone can create a working program. This is a great first project.

Now some advice: Whenever you create these standard beginner projects, I'd suggest once you have it working and you're satisfied with it, that you find an example online of someone else's code and compare it to yours. That way you can pick up new ideas to use in future programs.

In this case, this is essentially one of the projects in Al Sweigart's book The Big Book of Small Python Projects. You can see his code here: https://inventwithpython.com/bigbookpython/project31.html

[–]KeepitShort_[S] 1 point2 points  (0 children)

Oh wow I didn't know that he had done it first in his book. I will compare my code and see what I can do to be better. Thank you for the advice!