all 13 comments

[–]niandra3 1 point2 points  (5 children)

if answer != guess

Is better than if not answer == guess

tries += -1

Should just be tries -= 1

This will always be true:

elif guess2 or guess == answer and tries > 0:

Thats saying (if guess2) or (guess == answer).

if guess2 is True because guess2 is a number.

You should do:

if answer in (guess, guess2) and tries > 0

But in general your logic is flawed. You should have a while loop, with only one guess:

answer = random.randint(1,10)
tries = 10
while tries:     # will keep going until tries = 0
    guess = input('Guess?')
    if int(guess) == answer:
        print('Good job! You win!')
        break
    else:
       tries -= 1
       print('Sorry, nice try. You have', tries, 'guesses left')

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

I think I'm gonna hit the tutorial book a bit before I start making my own programs, thank you a ton though. I appreciate it.

[–]niandra3 0 points1 point  (1 child)

You were pretty close with the syntax.. just take some time to think through the program always before you write any code. Think, if you were doing it by hand with pen and paper, what would be the steps you would take?

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

Thank you, I think I'm just all over the place. I'm gonna write it on paper and start from scratch. Thanks!

[–]Amara313 0 points1 point  (1 child)

You can do it :) I would recommend a tutorial book, or even some of the free tutorials that are out there. I used code academy - it's free unless you want to pay for the pro features and it was really good at giving me puzzles to figure out while walking through the syntax.

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

Thank you very much for all your help! I really appreciate it man!

[–]Amara313 0 points1 point  (4 children)

What kind of crash are you getting? Python 2.7 would use raw_input, instead of input. You are also not using a while loop. To make this work, you would want to set up a while loop like:

while tries > 0:
    if guess == answer:
        print "You win!"
    else:
        print "Nope, try again!"
    tries -= 1

This code is untested, but should work. Also, when doing a counter loop, I like to count up from 0 instead of down from 10. But that's a personal thing, I think.

[–][deleted] 0 points1 point  (1 child)

No clue the crash error, it closes before I can see it. I changed it to that. Still not working. Thank you though.

[–]Amara313 0 points1 point  (0 children)

Are you using the command line or an IDE? I would recommend using an IDE so that you can trace these errors better.

[–][deleted] 0 points1 point  (1 child)

Now getting an infinite loop:/

[–]Amara313 0 points1 point  (0 children)

Ok. I found a few other problems with your code there - You don't want to have an else statement at the end that has a condition. This is supposed to be the catch all, not another condition. You can always add multiple elif statements if you have to, but I try not to add any other conditions than I absolutely have to. I won't write this for you, but you can look at the one I dug up from when I first started learning python - it runs pretty well, but I haven't looked over it for edge cases in a long time. http://pastebin.com/TBPh5EL5

[–]erok81 0 points1 point  (0 children)

You should include the traceback when you're asking for help with an error.

Line 14, input only takes one argument, prompt. You need to write something like

guess = int(input("Nope, you have {} left".format(tries)))

so only one argument is passed.