Thread: Operator Error

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

    Join Date
    Jan 2013
    Posts
    6
    Rep Power
    0

    Operator Error


    a = 10
    b = 2
    c = 2
    print ("1:Attack")
    print (r"2:Akorn Attack(Cost: 1 Akorn)")
    print ("3:Block")
    turn1 = input()
    if turn1 == 1:
    (a - 1)
    if turn1 == 2:
    (c - 1)
    (a - 3)
    if turn1 == 3:
    (b - 1)
    print (a)

    For some reason, this code, which is suposed to display a at the end, prints 10 when I enter one, but should't it print 9? Any info on why this is would be appreciated! thanks!
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2012
    Posts
    114
    Rep Power
    3
    Writing the expression a - 1 as a statement does not do anything useful. What you probably want is a -= 1.

    You really should consider giving your variables better names, too. Instead of "a", "b", and "c", perhaps "enemy_health", "your_health", and "akorns"? (Just guessing at their purposes here.)
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2013
    Posts
    6
    Rep Power
    0
    Originally Posted by Nyktos
    Writing the expression a - 1 as a statement does not do anything useful. What you probably want is a -= 1.

    You really should consider giving your variables better names, too. Instead of "a", "b", and "c", perhaps "enemy_health", "your_health", and "akorns"? (Just guessing at their purposes here.)
    We originally had that but we changed it. We will change their names back. hen i try to do what you suggested, it gives me the error of invalid syntax.
  6. #4
  7. Contributing User
    Devshed Demi-God (4500 - 4999 posts)

    Join Date
    Aug 2011
    Posts
    4,709
    Rep Power
    480
    Nyktos is python expert. Please post your code with syntax error for diagnosis.
    [code]Code tags[/code] are essential for python code and Makefiles!
  8. #5
  9. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2013
    Posts
    6
    Rep Power
    0
    My updated code that still doesn't work:
    playerhp = 10
    playerap = 2
    enemiehp = 5
    enemieap = 1
    print ("1: Attack")
    action = input()
    if action == 1:
    enemiehp - 2
    print (enemiehp)




    I enter 1 and it says 5, when it should be 4. Why is this?
  10. #6
  11. Contributing User
    Devshed Demi-God (4500 - 4999 posts)

    Join Date
    Aug 2011
    Posts
    4,709
    Rep Power
    480
    Code:
    playerhp = 10
    playerap = 2
    enemiehp = 5
    enemieap = 1
    print ("1: Attack")
    action = input()
    if action == 1:
        enemiehp = enemiehp - 2  # assign the result of the expression to your variable
    print (enemiehp)

    (note that Nyktos suggested something like
    enemiehp -= 2
    which is a short form of the statement I wrote)

    (note also that by subtracting TWO python correctly gets THREE.)
    [code]Code tags[/code] are essential for python code and Makefiles!
  12. #7
  13. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2013
    Posts
    6
    Rep Power
    0
    Thank you! EDIT: Still says its 5
  14. #8
  15. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2013
    Posts
    6
    Rep Power
    0
    Originally Posted by b49P23TIvg
    Code:
    playerhp = 10
    playerap = 2
    enemiehp = 5
    enemieap = 1
    print ("1: Attack")
    action = input()
    if action == 1:
        enemiehp = enemiehp - 2  # assign the result of the expression to your variable
    print (enemiehp)

    (note that Nyktos suggested something like
    enemiehp -= 2
    which is a short form of the statement I wrote)

    (note also that by subtracting TWO python correctly gets THREE.)
    I still get 5
  16. #9
  17. Contributing User
    Devshed Demi-God (4500 - 4999 posts)

    Join Date
    Aug 2011
    Posts
    4,709
    Rep Power
    480
    I suspect you're running your program in python3.

    input() returns a string.
    You should test

    if action == '1':

    (better than int(action) which would fail if action is not an integer)
    [code]Code tags[/code] are essential for python code and Makefiles!
  18. #10
  19. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2013
    Posts
    6
    Rep Power
    0
    Thank you!

IMN logo majestic logo threadwatch logo seochat tools logo