[Tutor] While problem

2013-06-26 Thread Jack Little
I have a small problem with the while function.It prints an odd variable that 
has nothing to do with the function. It prints Squad One!. Here is my code 
for the 3 def's that have something to do with the while function.



def tcombat():
    c1am=10
    c2am=10
    enam=Qasi
    ehealth=15
    edam=random.choice([5,6,7,8,9])
    thealth=20
    tdam=random.choice([6,7,8,9,10])
    enemyalive=True
    while enemyalive==True:   
        t2=raw_input()
        if t2.lower==FIRE CANNON 1:
            c1am-=c1am-1
            ehealth-tdam
            print Cannon Fired!
            print Enemy Health=, ehealth
            
        elif t2.lower==FIRE CANNON 2:
            c2am=c2am-1
            ehealth-tdam
            print Cannon Fired!
            print Enemy Health=, ehealth
    print Good Job!
    print You beat the training dummy.
    print Nothing like real combat
    print But screw you, here you go!
    print (Into real combat)
    lvl3()

def lvl2_2():
    print Squad Nine
    print This team looks veryumm..Dumb.
    print There is one guy sitting on a stool
    print he has a star sticker on his chest.
    print The other guy is vomiting on his
    print pants.
    print BEGIN TRAINING
    print TRAINING: When you are roaming around the skies, you type 'Engage 
[Ship Name]'
    print TRAINING: While in combat, type 'Fire [Cannon Number (not spelt)]' 
to fire a cannon at a ship
    print All entries must be in lower caps!
    print TRAINING: There may be consequences for firing upon certain ships.
    print --BEGIN TRAINING--
    print There is a ship near yours, the Qasi. It is flying
    print the enemy flag.
    print There are 2 cannons on your ship.
    c1am=10
    c2am=10
    enam=Qasi
    ehealth=15
    edam=random.choice([5,6,7,8,9])
    thealth=20
    tdam=random.choice([6,7,8,9,10])
    enemyalive=True
    if ehealth==0:
        enemyalive=False
    t1=raw_input()
    if t1.lower==engage qasi:
        print enam ,Engaged in Combat
        tcombat()
   
    

def lvl2_1():
    print Squad One
    print This team looks much more able than Squad Nine.
    print TYRONE:Hi! I'm Tyrone, he's James, she's Ashley, and that guy over 
there,
    print he's Bob.
    print BEGIN TRAINING
    print TRAINING: When you are roaming around the skies, you type 'Engage 
[Ship Name]'
    print TRAINING: While in combat, type 'Fire [Cannon Number (not spelt)]' 
to fire a cannon at a ship
    print TRAINING: There may be consequences for firing upon certain ships.
    print --BEGIN TRAINING--
    print There is a ship near yours, the Qasi. It is flying
    print the enemy flag.
    print There are 2 cannons on your ship.
    c1am=10
    c2am=10
    enam=Qasi
    ehealth=15
    edam=random.choice([5,6,7,8,9])
    thealth=20
    tdam=random.choice([6,7,8,9,10])
    enemyalive=True
    if ehealth==0:
        enemyalive=False
    t1=raw_input()
    if t1.lower==ENGAGE QASI:
        print Engaged in Combat
        tcombat()




Thanks!___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] While problem

2013-06-26 Thread Dave Angel

On 06/26/2013 08:32 PM, Jack Little wrote:

I have a small problem with the while function.It prints an odd variable


What variable is that?


that has nothing to do with the function. It prints Squad One!. Here is my 
code for the 3 def's that have something to do with the while function.




It's not at all clear what you want from us.  There aren't any while 
functions (since that would be a syntax error), though I do see a while 
statement in the tcombat() function.  Is that the one you're referring to?


I also see calls to functions you didn't provide, and since the logic 
makes no sense to me, it's not obvious whether they matter or not. 
Nothing here calls  lvl2_1(), so should we assume it's dead code?  For 
that matter, neither of the references to tcombat() will ever actually 
call it, so it's dead too?


Are you just looking for someone to correct your obvious mistakes?  Like 
the if statement and the elif statement that will never fire, because 
you forgot parentheses on the t2.lower method call ?  (And once you fix 
that, they'll still never fire, since you're then comparing uppercase to 
lowercase, and obviously they're different).


Because of that, the while statement will never terminate, just 
repeatedly asking for raw_input (with a prompt of ) and getting 
stuck till the user creates an exception, like Ctrl-C.




def tcombat():
 c1am=10
 c2am=10
 enam=Qasi
 ehealth=15
 edam=random.choice([5,6,7,8,9])
 thealth=20
 tdam=random.choice([6,7,8,9,10])
 enemyalive=True
 while enemyalive==True:
 t2=raw_input()
 if t2.lower==FIRE CANNON 1:
 c1am-=c1am-1
 ehealth-tdam
 print Cannon Fired!
 print Enemy Health=, ehealth

 elif t2.lower==FIRE CANNON 2:
 c2am=c2am-1
 ehealth-tdam
 print Cannon Fired!
 print Enemy Health=, ehealth
 print Good Job!
 print You beat the training dummy.
 print Nothing like real combat
 print But screw you, here you go!
 print (Into real combat)
 lvl3()


Does lvl3() look anything like  lvl2_2() below?  If so, you're looking 
for trouble, recursively calling between tcombat() and the various other 
functions.  Eventually, the stack fills up.  A function call is not a 
goto statement.




def lvl2_2():
 print Squad Nine
 print This team looks veryumm..Dumb.
 print There is one guy sitting on a stool
 print he has a star sticker on his chest.
 print The other guy is vomiting on his
 print pants.
 print BEGIN TRAINING
 print TRAINING: When you are roaming around the skies, you type 'Engage [Ship 
Name]'
 print TRAINING: While in combat, type 'Fire [Cannon Number (not spelt)]' to 
fire a cannon at a ship
 print All entries must be in lower caps!
 print TRAINING: There may be consequences for firing upon certain ships.
 print --BEGIN TRAINING--
 print There is a ship near yours, the Qasi. It is flying
 print the enemy flag.
 print There are 2 cannons on your ship.
 c1am=10
 c2am=10
 enam=Qasi
 ehealth=15
 edam=random.choice([5,6,7,8,9])
 thealth=20
 tdam=random.choice([6,7,8,9,10])


You never use the values of edam, thealth, and tdam.  So why calculate them?


 enemyalive=True
 if ehealth==0:
 enemyalive=False


This statement and the similar one above did nothing useful. You never 
check the value of enemyalive in this function.



 t1=raw_input()
 if t1.lower==engage qasi:
 print enam ,Engaged in Combat
 tcombat()



def lvl2_1():
 print Squad One
 print This team looks much more able than Squad Nine.
 print TYRONE:Hi! I'm Tyrone, he's James, she's Ashley, and that guy over 
there,
 print he's Bob.
 print BEGIN TRAINING
 print TRAINING: When you are roaming around the skies, you type 'Engage [Ship 
Name]'
 print TRAINING: While in combat, type 'Fire [Cannon Number (not spelt)]' to 
fire a cannon at a ship
 print TRAINING: There may be consequences for firing upon certain ships.
 print --BEGIN TRAINING--
 print There is a ship near yours, the Qasi. It is flying
 print the enemy flag.
 print There are 2 cannons on your ship.
 c1am=10
 c2am=10
 enam=Qasi


You never use enam.  It looks useful, so how did you expect to be using it?


 ehealth=15
 edam=random.choice([5,6,7,8,9])
 thealth=20
 tdam=random.choice([6,7,8,9,10])
 enemyalive=True
 if ehealth==0:
 enemyalive=False


This local variable is never referenced.  So why set it?  Of course that 
doesn't really matter, since ehealth is not going to be zero;  it's 
initialized right above to 15.



 t1=raw_input()
 if t1.lower==ENGAGE QASI:


This will never be equal, so the following never happens.


 print Engaged in Combat
 tcombat()





--
DaveA
___
Tutor 

Re: [Tutor] While problem

2013-06-26 Thread Steven D'Aprano

On 27/06/13 10:32, Jack Little wrote:

I have a small problem with the while function.It prints an odd variable that has nothing 
to do with the function. It prints Squad One!. Here is my code for the 3 
def's that have something to do with the while function.


But you don't actually show us the while loop that prints Squad One!. That's 
rather less than useful. How do you expect us to fix the broken code without seeing it?

Hello Mr Mechanic, I have a car that is making a strange noise when I turn left. 
Rather than bring that car in for you to look at, I thought I'd bring in the three cars 
that are parked next to it, just in case the problem is with them...

:-)



Some unrelated comments below:



def tcombat():
 c1am=10
 c2am=10
 enam=Qasi
 ehealth=15
 edam=random.choice([5,6,7,8,9])


edam? Like the cheese?



 thealth=20
 tdam=random.choice([6,7,8,9,10])
 enemyalive=True
 while enemyalive==True:


You don't need to say while enemyalive == True, since enemyalive is already a true or 
false value. Just say while enemyalive:.



 t2=raw_input()
 if t2.lower==FIRE CANNON 1:


This cannot every succeed, since you are comparing the *method* (like a function) 
t2.lower with the *string* FIRE CANNON 1. You need to actually *call* the 
method, to get a result:

if t2.lower() == FIRE CANNON 1:

which of course also can never succeed, since you're comparing a lowercase 
string with an UPPERCASE string. You need one of these instead:

if t2.lower() == fire cannon 1:

if t2.upper() == FIRE CANNON 1:



 c1am-=c1am-1


If you think about this mathematically, you will see that this cannot fail to set c1am to 
1. If that's what you intended, just write c1am = 1. Or if you meant to 
subtract 1 from c1am, then you can write either of these:

c1am = c1am - 1

c1am -= 1

My suggestion is that you are less likely to make these sorts of errors if you 
put spaces around equal signs and other operators. Spaces make things easier to 
read, or another way to put it, notusingspacesmakesthingsmuchhardertoread.


(By the way, I hope these variable names mean something to you, because most of them mean 
absolutely nothing to me. c1am? WTH does that stand for?)



 ehealth-tdam


This line is useless, since it just calculates the value ehealth - tdam, then 
throws the result away unused. Perhaps you meant this?

ehealth -= tdam



 print Cannon Fired!
 print Enemy Health=, ehealth

 elif t2.lower==FIRE CANNON 2:


The same flaw applies here as above.



 c2am=c2am-1
 ehealth-tdam


Likewise.



 print Cannon Fired!
 print Enemy Health=, ehealth
 print Good Job!
 print You beat the training dummy.
 print Nothing like real combat
 print But screw you, here you go!
 print (Into real combat)
 lvl3()

def lvl2_2():
 print Squad Nine
 print This team looks veryumm..Dumb.
 print There is one guy sitting on a stool
 print he has a star sticker on his chest.
 print The other guy is vomiting on his
 print pants.
 print BEGIN TRAINING
 print TRAINING: When you are roaming around the skies, you type 'Engage [Ship 
Name]'
 print TRAINING: While in combat, type 'Fire [Cannon Number (not spelt)]' to 
fire a cannon at a ship
 print All entries must be in lower caps!


Lower caps? Do you mean lower case? ALL CAPS?



 print TRAINING: There may be consequences for firing upon certain ships.


Oh good. Consequences. Are they good consequences or bad consequences?



By the way, your code contains an awful lot of duplicated code. You should pull 
out the duplicated code and put it into functions, then pass an appropriate 
variable to the function. A simplified example follows.

Instead of this duplicated code:

def squad_one():
print This is squad one.
print You're training
print Do this
print Do that
print Do something else

def squad_two():
print This is squad two.
print You're training
print Do this
print Do that
print Do something else


if squad == squad one:
squad_one()
else:
squad_two()



you can instead do this:


def squad(name):
print This is Squad %s. % name
print You're training
print Do this
print Do that
print Do something else

if squad == squad one:
squad(one)
else:
squad(two)



--
Steven
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor