#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2012
    Posts
    3
    Rep Power
    0

    Post Random Card Draw Game


    Hi, I'm reading a beginner's book in making games with Python and finding the language pretty friendly. I had no problem with the examples, so I tried writing my own random card game, but keep getting "invalid syntax" when I try to run it. I cannot figure out the problem, so if anyone could give me a hint, I would really appreciate it. Thanks much in advance!

    Code:
    #This is a random card draw game. import random cardsdrawn = 0 print('Hello, let\'s play cards! The person whose card has more hearts wins. I\'ll deal for you.') while cardsdrawn < 10: number = random.randint(1,12) str_number = str(number) print('Oh, you got the card of ' + str_number + ' hearts.' cardsdrawn = cardsdrawn + 1 if number < 8: print('Too bad, your card is below my number. Haha.') if number < 9: print('So close! My card has one more heart than yours.') if number >= 10: break if number >= 10: print('Good job! You won the game!') if number != 10: print('Sorry, I won.')
  2. #2
  3. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2012
    Posts
    3
    Rep Power
    0
    Uh, I feel like an idiot, but how do you get code to appear as it was written? The code tags don't seem to the best option...
  4. #3
  5. Contributing User
    Devshed Demi-God (4500 - 4999 posts)

    Join Date
    Aug 2011
    Posts
    4,841
    Rep Power
    480
    Code:
    #This is a random card draw game.
    import random
    cardsdrawn = 0
    print('Hello, let\'s play cards! The person whose card has more hearts wins. I\'ll deal for you.')
    while cardsdrawn < 10:
        number = random.randint(1,12)
        str_number = str(number)
        ################################################################
        print('Oh, you got the card of ' + str_number + ' hearts.')
        #                                                         ^ your program lacks this `)'
        ################################################################
        cardsdrawn = cardsdrawn + 1
        if number < 8:
            print('Too bad, your card is below my number. Haha.')
        if number < 9:
            print('So close! My card has one more heart than yours.')
        if number >= 10:
            break
    # (I have no good guess about the intent of the following code.)
    if 10 < number:
        print('Good job! You won the game!')
    elif 10 == number:
        print('Sorry, I won.')
    else:
        print('this arbitrary game blows moose meat.')
    [code]Code tags[/code] are essential for python code and Makefiles!
  6. #4
  7. Lord of the Dance
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Oct 2003
    Posts
    3,609
    Rep Power
    1945
    Originally Posted by merondi
    Uh, I feel like an idiot, but how do you get code to appear as it was written? The code tags don't seem to the best option...
    copy the code into the advance editor, mark the code and then click on the code-button.
    This will give you the result as in above post.
  8. #5
  9. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2012
    Posts
    33
    Rep Power
    3
    Code:
    #This is a random card draw game.
    import random
    cardsdrawn = 0
    print('Hello, let\'s play cards! The person whose card has more hearts wins. I\'ll deal for you.')
    while cardsdrawn < 10:
        number = str(random.randint(1,12)) # you are able to run a function in a function 
        ################################################################
        print('Oh, you got the card of %r of hearts.', number) # %r is a raw format or repr()
        #                                                         ^ your program lacks this `)'
        ################################################################
        cardsdrawn += 1 # += what ever number is after it in this case 1
        if number < 8:
            print('Too bad, your card is below my number. Haha.')
        if number < 9:
            print('So close! My card has one more heart than yours.')
        if number >= 10:
            break
    # (I have no good guess about the intent of the following code.)
    if 10 < number:
        print('Good job! You won the game!')
    elif 10 == number:
        print('Sorry, I won.')
    else:
        print('this arbitrary game blows moose meat.')
    alright not too bad i believe these little changes might be able to help you with your code, i am not to get with python 3, let me know
  10. #6
  11. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2012
    Posts
    3
    Rep Power
    0
    Thank you all for the advice! I'm getting the code to partially run, though I guess there's still some kinks to work out. I admit it may be an arbitrary game, but hey, it's good practice. At least it's running now, so thanks again.
    Last edited by merondi; October 10th, 2012 at 03:01 PM. Reason: misspelling
  12. #7
  13. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2012
    Posts
    33
    Rep Power
    3
    sorry i should have mentioned that %r is only used inside strings there are quite a few like %r such as %d for an int %f for a float %.xf for a float with x amount of decimal places, look them up it will help you greatly

    be sure that you stick to strict formatting of you code, to make it easier for you and others to read so if you like coding like that then don't change that anyway, if you want any more questions answer ask me on the forums or simply ask the forum

IMN logo majestic logo threadwatch logo seochat tools logo