1

I'm somewhat new to Python 2.7 and I tried created a simple dice game however I'm having some difficulty getting the computer to recognize y, as no matter what I do it will not recognize answer as being y or n. Here is the code below.

import random
a = random.randint(1, 6)
b = random.randint(1, 6)
points = 0
strikes = 0

def main():
    print """Welcome to the two dice game. Depending on the result, you will earn a point or a strike. Three strikes end the game. Good luck!"""
    anwser = input('Play? y for yes, n for no')
    if anwser == "y":
       print a
       print b
       if a == b:
          print ("Congrats! You earned a point!")
          points = points + 1
          main()
    else:
        print("That's a strike...")
        strikes = strikes + 1
        if strikes == 3:
            print("That's three strikes, game over.")
            break
        else:
            main()
    if anwser == n:
       print ("Game over. You earned this many points.")
       print points

main()
  • 1
    The correct spelling is 'answer' (just to let you know). You need to use raw_input instead of input because it is python 2x not 3x. –  Jun 25 '15 at 16:45
  • Another thing to note is that recursing on `main` is probably not what you want to do. Try using a `while` loop instead (`def main(): while strikes < 3: `) – Adam Smith Jun 25 '15 at 16:49
  • You use three different types of print statement formats: one with 3 quotes at the beginning, one with no parenthesis, and one with. I would recommend surrounding all your `print` methods with parenthesis so it is consistent and because it's easier to port and run in python3. Example: `print(a)` Also, your "main()" section at the bottom should have this in it: https://docs.python.org/3/library/__main__.html – NuclearPeon Jun 25 '15 at 16:49
  • Wow, thanks for the help. Didn't realize I screwed up that bad. – jayfeather31 Jun 25 '15 at 16:53
  • Not screwups. Learning opportunities. Are you using a tutorial on Python 2? If you're just starting out, why not learn Python 3 instead? – leewz Jun 25 '15 at 17:12
  • I did, I just found 2.7 to be far more effective than 3.1 for some reason. – jayfeather31 Jun 25 '15 at 19:52

2 Answers2

6

input() actually tries to evaluate whatever you pass to it. You want raw_input().

See this question for more information on input() vs raw_input().

Community
  • 1
  • 1
afontaine
  • 1,059
  • 9
  • 9
0

I find getting input with python 2 works well using raw_input as taught at Codecademy. With python 3 its input() function works well. There was a problem accessing strikes and points which should be declared global in main() and it may be better to put a and b into main() so users re-roll the dice when main() reruns itself otherwise they get stuck with the same roll that either terminates in 3 plays or never terminates. The if-then logic can use some fixing so that a "n" answer terminates the game as suggested below along with some other changes such as formatting the initial message so it doesn't print beyond a normal sized window:

import random
points = 0
strikes = 0

def main():
    a = random.randint(1, 6)
    b = random.randint(1, 6)
    global points
    global strikes
    print """Welcome to the two dice game. Depending on the result
you will earn a point or a strike. Three strikes end 
the game. Good luck!"""
    answer = raw_input('Play? y for yes, n for n: ')
    if answer == "y":
        print a
        print b
        if a == b:
            print ("Congrats! You earned a point!")
            points = points + 1
            main()
        else:
            print("That's a strike...")
            strikes = strikes + 1
            if strikes == 3:
                print("That's three strikes, game over.")
                exit(1)
            else:
                main()
    elif answer == "n":
        print ("Game over. You earned this many points.")
        print points

main()

It may be interesting to offset strikes with points by calculating strikes as strikes -= points and on termination with "n" to offset points with strikes with points -= strikes.

Codecademy has free all hands-on training for Python 2. I recently went through it and its good. For Python 3, Introducing Python by Bill Lubanovic is excellent because it starts at a beginners level and progresses to intermediate expertise, is very readable, has exercises at the end of chapters with solutions in an appendix and code on github and its relatively short compared to some Python tomes (Learning Python and Programming Python for example) -- it covers core Python 3 in 7-9 chapters in 200-250 pages and the rest are on more specialized and optional but interesting areas such as NoSQL database access, web services and style.