Python Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me

The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.

Go Back   Dev Shed ForumsProgramming LanguagesPython Programming

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old January 6th, 2013, 03:59 PM
Sebomon Sebomon is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jan 2013
Posts: 6 Sebomon User rank is Just a Lowly Private (1 - 20 Reputation Level) 
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!

Reply With Quote
  #2  
Old January 6th, 2013, 04:10 PM
Nyktos Nyktos is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Dec 2012
Posts: 76 Nyktos User rank is Corporal (100 - 500 Reputation Level)Nyktos User rank is Corporal (100 - 500 Reputation Level)Nyktos User rank is Corporal (100 - 500 Reputation Level)Nyktos User rank is Corporal (100 - 500 Reputation Level) 
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.)

Reply With Quote
  #3  
Old January 6th, 2013, 04:23 PM
Sebomon Sebomon is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jan 2013
Posts: 6 Sebomon User rank is Just a Lowly Private (1 - 20 Reputation Level) 
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.

Reply With Quote
  #4  
Old January 6th, 2013, 05:33 PM
b49P23TIvg's Avatar
b49P23TIvg b49P23TIvg is offline
Contributing User
Dev Shed Loyal (3000 - 3499 posts)
 
Join Date: Aug 2011
Posts: 3,383 b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level) 
Time spent in forums: 1 Month 2 Weeks 3 Days 13 h 44 m 42 sec
Reputation Power: 383
Nyktos is python expert. Please post your code with syntax error for diagnosis.
__________________
[code]Code tags[/code] are essential for python code!

Reply With Quote
  #5  
Old January 6th, 2013, 07:39 PM
Sebomon Sebomon is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jan 2013
Posts: 6 Sebomon User rank is Just a Lowly Private (1 - 20 Reputation Level) 
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?

Reply With Quote
  #6  
Old January 6th, 2013, 07:48 PM
b49P23TIvg's Avatar
b49P23TIvg b49P23TIvg is offline
Contributing User
Dev Shed Loyal (3000 - 3499 posts)
 
Join Date: Aug 2011
Posts: 3,383 b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level) 
Time spent in forums: 1 Month 2 Weeks 3 Days 13 h 44 m 42 sec
Reputation Power: 383
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.)

Reply With Quote
  #7  
Old January 6th, 2013, 07:51 PM
Sebomon Sebomon is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jan 2013
Posts: 6 Sebomon User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 h 42 m 48 sec
Reputation Power: 0
Thank you! EDIT: Still says its 5

Reply With Quote
  #8  
Old January 6th, 2013, 07:55 PM
Sebomon Sebomon is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jan 2013
Posts: 6 Sebomon User rank is Just a Lowly Private (1 - 20 Reputation Level) 
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

Reply With Quote
  #9  
Old January 6th, 2013, 08:09 PM
b49P23TIvg's Avatar
b49P23TIvg b49P23TIvg is offline
Contributing User
Dev Shed Loyal (3000 - 3499 posts)
 
Join Date: Aug 2011
Posts: 3,383 b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level) 
Time spent in forums: 1 Month 2 Weeks 3 Days 13 h 44 m 42 sec
Reputation Power: 383
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)

Reply With Quote
  #10  
Old January 6th, 2013, 08:18 PM
Sebomon Sebomon is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jan 2013
Posts: 6 Sebomon User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 h 42 m 48 sec
Reputation Power: 0
Thank you!

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesPython Programming > Operator Error

Developer Shed Advertisers and Affiliates



Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.

© 2003-2013 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap