The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.
|
 |
|
Dev Shed Forums
> Programming Languages
> Python Programming
|
Operator Error
Discuss Operator Error in the Python Programming forum on Dev Shed. Operator Error Python Programming forum discussing coding techniques, tips and tricks, and Zope related information. Python was designed from the ground up to be a completely object-oriented programming language.
|
|
 |
|
|
|
|

Dev Shed Forums Sponsor:
|
|
|

January 6th, 2013, 03:59 PM
|
|
Registered User
|
|
Join Date: Jan 2013
Posts: 6
Time spent in forums: 1 h 42 m 48 sec
Reputation 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!
|

January 6th, 2013, 04:10 PM
|
|
Contributing User
|
|
Join Date: Dec 2012
Posts: 76
  
Time spent in forums: 1 Day 3 h 26 m 15 sec
Reputation Power: 2
|
|
|
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.)
|

January 6th, 2013, 04:23 PM
|
|
Registered User
|
|
Join Date: Jan 2013
Posts: 6
Time spent in forums: 1 h 42 m 48 sec
Reputation Power: 0
|
|
Quote: | 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.
|

January 6th, 2013, 05:33 PM
|
 |
Contributing User
|
|
|
|
|
Nyktos is python expert. Please post your code with syntax error for diagnosis.
__________________
[code] Code tags[/code] are essential for python code!
|

January 6th, 2013, 07:39 PM
|
|
Registered User
|
|
Join Date: Jan 2013
Posts: 6
Time spent in forums: 1 h 42 m 48 sec
Reputation 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?
|

January 6th, 2013, 07:48 PM
|
 |
Contributing User
|
|
|
|
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.)
|

January 6th, 2013, 07:51 PM
|
|
Registered User
|
|
Join Date: Jan 2013
Posts: 6
Time spent in forums: 1 h 42 m 48 sec
Reputation Power: 0
|
|
|
Thank you! EDIT: Still says its 5
|

January 6th, 2013, 07:55 PM
|
|
Registered User
|
|
Join Date: Jan 2013
Posts: 6
Time spent in forums: 1 h 42 m 48 sec
Reputation Power: 0
|
|
Quote: | 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
|

January 6th, 2013, 08:09 PM
|
 |
Contributing User
|
|
|
|
|
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)
|

January 6th, 2013, 08:18 PM
|
|
Registered User
|
|
Join Date: Jan 2013
Posts: 6
Time spent in forums: 1 h 42 m 48 sec
Reputation Power: 0
|
|
|
Thank you!
|
Developer Shed Advertisers and Affiliates
| Thread Tools |
Search this Thread |
|
|
|
| Display Modes |
Rate This Thread |
Linear Mode
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|
|