[Tutor] While problem

Dave Angel davea at davea.name
Thu Jun 27 03:04:32 CEST 2013


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 very....umm..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


More information about the Tutor mailing list