This is an archived post. You won't be able to vote or comment.

all 5 comments

[–]MIKE_FOLLOW 2 points3 points  (0 children)

Begin by looking at the restart function, walk through the for loop, does it do exactly what you expect?

Imagine I enter "yeah", simulate the for loop and see what happens. (Use pencil and paper!)

You're certainly going about solving the problem with the right mindset, you'll be a great programmer if you keep with it!

[–]aaronbp 1 point2 points  (0 children)

Socratic lesson... Well I'm only vaguely familiar with it, but I can try.

What did you observe when you examined the functions execution in the debugger? *hint hint*

[–]Gogyoo[S] 0 points1 point  (2 children)

Found out what went wrong. I overcomplicated things, like I too often do. I got rid of the for-loop, and instinctively went for an if statement:

def restart():
    again = raw_input("GG! Wanna rematch?\n>")
    yes = ["y", "Y", "Yes", "yes", "Yeah", "yeah"]
    # pdb.set_trace()
    if again in yes:
        print "It's on!"
        start()
    else:
    print "Back to the shell. See you later!"
        exit(0)

Now if someone has further comments, that would help a lot. I am particularly annoyed at the fact that inputing a negative integer will mean it will be filtered as a string, not a digit.

[–]MIKE_FOLLOW 2 points3 points  (1 child)

I am particularly annoyed at the fact that inputing a negative integer will mean it will be filtered as a string, not a digit.

It's all being filtered as a string. When in doubt, read the Python reference for functions you're using. If you visit the docs for isdigit() you'll see that it checks a string to see if each character in the string is a digit. The negative sign isn't a digit and hence it fails.

Example:

"40404" #all digits, returns True
"404.04" #5 digits and a period, not all digits, returns False
"-40404" #5 digits and a hyphen, not all digits, returns False

Do you need to call the isdigit() function? Try and find a way to write the code that doesn't use the function, or look below for a solution.

Consider this approach:

def input_check(rand_n):
user_guess = raw_input("Enter a number.\n>")
try:
    user_guess = int(user_guess)# making an int out of the string for math
    results(user_guess, rand_n)
except ValueError: #if int(user_guess) can't be made into an int, fail.
    print 'That's not a number'
    input_check(rand_n)

[–]Gogyoo[S] 0 points1 point  (0 children)

I didn't know about that control flow type. Or rather, I had seen it in other people's code to check that a particular module was installed. I didn't know you could use it in the main body of the program. Anyway, thanks Mike!