[Tutor] While problem
Steven D'Aprano
steve at pearwood.info
Thu Jun 27 03:16:52 CEST 2013
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 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!"
"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
More information about the Tutor
mailing list