new to While statements


K

krismesenbrink

import random



def room ():

hp = 10
while hp != 0:

random_Number = random.randint(1, 2)

#asking if you want to roll/play
des = input("Would you like to roll the die?")

if des == ("y"):
print ("Rolling the die...")
print ("You rolled a...")
print (random_Number)

#a "monster" appers if you roll a 1""
if random_Number == 1:
monster_hp = 10
print ("Oh no a Monsster!")
print ("would you like to attack?")
answer = input("y or n?")
if answer == "y":
#if you choose to battle this is what happens
while monster_hp >=0:
print ("you attack")
damage_done = random.randint(0,5)
print ("You do ",damage_done,"damage")
monster_hp = monster_hp - damage_done
print ("the monster takes a hit, it has ", monster_hp,
"left")


elif answer == ("n"):
print ("you run away")

else:
print ("You and the monster just stare at one another")
else:
print ("You find nothing")
# if you decisde to not play it will kill you
elif des == ("no"):
hp = 0
print ("Maybe next time!")
else:
print ("please enter yes or no")

room()


this is the code i'm making. as the subject says im new to while statements.. i am having problems with the monster battle part, it takes health away from the "monster" but as soon as it gets to 0 or less i'd like the code to start from the top and ask you to roll the die again. any help on this would be greatly appreciative
 
Ad

Advertisements

S

snakeinmyboot

Hey there, cool idea you've got going on here! As far as I can tell though...what you want to happen, is indeed actually happening. Did you mean something else? Everytime I run the script and defeat a monster, it asks me if I want to roll the dice again.
 
K

krismesenbrink

and it seems you are right about that, i don't know what was wrong with my IDE before, i closed it and opened it up again,seemed to fix the problem. thanks for taking the time to look at it anyway!
 
S

snakeinmyboot

yea no problem. heres a little tip though so you atleast get something out of the post.

monster_hp = monster_hp - damage_done

can be simplified by writing

monster_hp -= damage_done

the -= means equal to whatever is on the left, minus whatevers on the right. this can be done with addition, multiplication, division, etc etc etc.
 
J

Joshua Landau

import random



def room ():

No need for the space after "room".
hp = 10
while hp != 0:

"while hp:" would be idiomatic, but you probably want "while hp > 0"
if you allow negatives.
random_Number = random.randint(1, 2)

You meant "random_number", I'm sure ;).
#asking if you want to roll/play

Don't comment lines unless the comment says more than the line.
des = input("Would you like to roll the die?")

if des == ("y"):

You don't need the brackets. Try to avoid redundant brackets where
they don't clarify anything.

Additionally, you should probably be more lenient and consistent with
answers. Maybe something like:

if des.casefold() in ("y", "yes"):

and its counterpart

if des.casefold() in ("n", "no"):
print ("Rolling the die...")
print ("You rolled a...")
print (random_Number)

You should avoid spaces between the function and the brackets.
#a "monster" appers if you roll a 1""
if random_Number == 1:
monster_hp = 10
print ("Oh no a Monsster!")
print ("would you like to attack?")
answer = input("y or n?")
if answer == "y":
#if you choose to battle this is what happens
while monster_hp >=0:

Think carefully here -- do you want to have a round when monster_hp is
greater *or equal* to 0? Maybe you would rather only if it's alive (hp
print ("you attack")
damage_done = random.randint(0,5)
print ("You do ",damage_done,"damage")
monster_hp = monster_hp - damage_done

monster_hp -= damage_done
print ("the monster takes a hit, it has ", monster_hp,
"left")

elif answer == ("n"):
print ("you run away")

else:
print ("You and the monster just stare at one another")
else:
print ("You find nothing")
# if you decisde to not play it will kill you
elif des == ("no"):
hp = 0

Gah! You just kill him off? Are you sure you don't want to use a
"break" or "return" to quit the loop or function?
print ("Maybe next time!")
else:
print ("please enter yes or no")

room()


As a whole, +1 for the good naming, decent attempt at spacing and a
mostly-sane layout.

this is the code i'm making. as the subject says im new to while statements. i am having problems with the monster battle part, it takes health awayfrom the "monster" but as soon as it gets to 0 or less i'd like the code to start from the top and ask you to roll the die again. any help on this would be greatly appreciative


PS: I'd use a state machine for times like this, eg.

import random

def prompt(action, retort_invalid=None):
while "asking for reponse":
response = input("Would you like to {}?".format(action)).casefold()

if response in ["y", "yes"]:
return True

elif response in ["n", "no"]:
return False

else:
if retort_invalid is not None:
print(retort_invalid)

def room():
state = "wandering"

while "adventuring":

if state == "idle":
if prompt("wander around")
print("Rolling the die...")

roll = random.randint(1, 2)

print("You rolled a {}.".format(roll))

if roll == 1:
monster_hp = 10
print("Oh no! A Monster!")
state = "facing monster"

else:
print("You find nothing")

else:
print("Maybe next time!")
return

elif state == "facing monster":
will_attack =

if prompt("attack", "You and the monster just stare at
one another"):
state = "attacking"

else:
print("you run away")
state = "idle"

elif state == "attacking":
damage_done = random.randint(0, 5)
monster_hp -= damage_done

print("You attack to do", damage_done, "damage")
print("The monster takes a hit, it has", monster_hp, "hp left")

if monster_hp <= 0:
state = "idle"

room()


The advantage of this is everything sits at the same level so you know
whether you've covered all options and permutations of options. It's
only longer because I made the "prompt" function which is more
rigorous than the current method.
 
Ad

Advertisements

K

krismesenbrink

wow everyone thanks for the feed back! i'll have to rewrite this with everything you guys taught me. this makes ALOT more sense. :D
 
V

Vito De Tullio

Dan said:
Funny, perhaps, the first time you see it, but way more informative than
the other way to the next one who comes along and reads it.

While I understand that it's syntactically and semantically correct, my nose
still doesn't like it.

It's not that's just not common... I just think it's a mishmash... it's not
a "rule" thing, more a "feeling wrong" one.

Maybe it's better if I try to explain in another way...

My first instinct about it it's to think why the author choose this way to
talk to the reader.

This message it's clearly meant to be read by another programmer, not by the
user. But in my mind it should be a comment. In my mind code explain "what"
is happening, comment say "why". (Sometimes talking to my colleagues we say
comment is a way to ask to forgiveness for a particular obscure code, as in
"sorry, I needed to flip-this and flop-that for such-and-such reason")

Following this reasoning, I will found more readable something like

# asking for response
while True:
...

that

while 'asking for response':
...

because in the latter case the "why" and the "how" are mixed. It's like
you're talking with the interpreter, but the message is for the programmer..

I hope I explained myself...
 
Ad

Advertisements

D

Dave Angel

Vito said:
While I understand that it's syntactically and semantically correct, my nose
still doesn't like it.

Neither does mine. There's no need for a trick here. "while True"
reads better, and a comment (on the same line, preferably) can explain
what the loop is for.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top