Help with a while loop

This is the place for queries that don't fit in any of the other categories.

Help with a while loop

Postby hootdoog » Wed Dec 04, 2013 5:35 pm

Hey guys,
I'm currently trying to create a text based game similar to that of zork, however I am having some trouble with the while loop, I have no clue how to make it stop once the enemy' health reaches zero, I have tried for around and hour and a half now and it doesn't seem to work, any ideas?

Code: Select all
import random
rounds = 0
health = 100
enemy = 50
print (" -==Welcome To TurkeyQuest==- ")
print ("")
print ("")
name = input("What is your name young turkey: ")
print ("")
print ("Thats a stupid name", name)
print ("")
weapon = input(" To your left you see a sword, to your right a bow what do you choose: ")
if weapon == "Sword":
    print ("")
    print ("Haha, swords suck!")
    print ("")
elif weapon == "Bow":
    print ("")
    print("Lol, Bows suck!")
    print ("")
elif weapon == "sword":
    print ("")
    print ("Haha, swords suck!")
    print ("")
elif weapon == "bow":
    print ("")
    print("Lol, Bows suck!")
else:
    print ("Choose a real weapon!")
print ("")
print("Look a Hopscotch bandit!")
attack = "Attack"
#attack = input("What do you! [Attack, Run]: ")
print ("")
#if attack == "Attack":
    #while enemy >1 or health >0 or attack == "run":  # the more than or less than is an indicator not a guideline
while attack == "Attack":
        enemy = enemy - random.randint(1,15)
        print ("You attack with your", weapon)
        print ("Enemy Health: ", enemy )
        print ("")
        health = health - random.randint(1,10) # WOO SPECIAL RANDOM THINGS YEAH WOO IT WORKS!!!!! (Note: Im really happy I got that to work!)
        print ("Enemy attacks")
        print ("Your Health: ", health )
        print ("")
        attack = input("What do you! [Attack, Run]: ")
        print ("")
if enemy == "0":
    print ("you slay the enemy with grace!")
Last edited by Yoriz on Wed Dec 04, 2013 5:38 pm, edited 1 time in total.
Reason: First post lock
hootdoog
 
Posts: 14
Joined: Wed Dec 04, 2013 5:22 pm

Re: Help with a while loop

Postby Yoriz » Wed Dec 04, 2013 5:52 pm

You can use break to stop a while loop.
Due to the reasons discussed here we will be moving to python-forum.io/ on October 1 2016
This forum will be locked down and no one will be able to post/edit/create threads, etc. here from thereafter. Please create an account at the new site to continue discussion.
User avatar
Yoriz
 
Posts: 1672
Joined: Fri Feb 08, 2013 1:35 am
Location: UK

Re: Help with a while loop

Postby hootdoog » Wed Dec 04, 2013 6:03 pm

Yoriz wrote:You can use break to stop a while loop.

Thanks for the reply,
I know that breaks can be used however I just want the code to stop looping and print the bottom line when the enemys health reaches zero :D

many Thanks, Rhys
hootdoog
 
Posts: 14
Joined: Wed Dec 04, 2013 5:22 pm

Re: Help with a while loop

Postby Yoriz » Wed Dec 04, 2013 6:06 pm

Check inside the while loop if the enemys health is 0 or less and break out of the loop.
Due to the reasons discussed here we will be moving to python-forum.io/ on October 1 2016
This forum will be locked down and no one will be able to post/edit/create threads, etc. here from thereafter. Please create an account at the new site to continue discussion.
User avatar
Yoriz
 
Posts: 1672
Joined: Fri Feb 08, 2013 1:35 am
Location: UK

Re: Help with a while loop

Postby hootdoog » Wed Dec 04, 2013 6:10 pm

Yoriz wrote:Check inside the while loop if the enemys health is 0 or less and break out of the loop.

I have tried using an if statement just after the loop but it is is not working?

Code: Select all
        attack = input("What do you! [Attack, Run]: ")
        print ("")
        enemy == "<1":
            break
                print ("you slay the enmy with grace!")
hootdoog
 
Posts: 14
Joined: Wed Dec 04, 2013 5:22 pm

Re: Help with a while loop

Postby Yoriz » Wed Dec 04, 2013 6:19 pm

You are checking for the value of enemy to be the same as a string "<1"
try checking for a less than or equal to number condition.
Code: Select all
enemy <= 0:
Due to the reasons discussed here we will be moving to python-forum.io/ on October 1 2016
This forum will be locked down and no one will be able to post/edit/create threads, etc. here from thereafter. Please create an account at the new site to continue discussion.
User avatar
Yoriz
 
Posts: 1672
Joined: Fri Feb 08, 2013 1:35 am
Location: UK

Re: Help with a while loop

Postby hootdoog » Wed Dec 04, 2013 6:22 pm

Yoriz wrote:You are checking for the value of enemy to be the same as a string "<1"
try checking for a less than or equal to number condition.
Code: Select all
enemy <= 0:


Im now getting a syntax error ://

Code: Select all
        attack = input("What do you! [Attack, Run]: ")
        print ("")
        enemy == "<= 0":
            break
            print ("you slay the enmy with grace!")
hootdoog
 
Posts: 14
Joined: Wed Dec 04, 2013 5:22 pm

Re: Help with a while loop

Postby Yoriz » Wed Dec 04, 2013 6:39 pm

When you get an error show the full traceback, please read the 'new user read this' link in my signature for infomration on how to ask good questions.
Code: Select all
enemy == "<= 0":
is not the same as
Code: Select all
enemy <= 0:

you have turned the answer into another number equals string condition check also you are missing the if at the begining.
Due to the reasons discussed here we will be moving to python-forum.io/ on October 1 2016
This forum will be locked down and no one will be able to post/edit/create threads, etc. here from thereafter. Please create an account at the new site to continue discussion.
User avatar
Yoriz
 
Posts: 1672
Joined: Fri Feb 08, 2013 1:35 am
Location: UK

Re: Help with a while loop

Postby stranac » Wed Dec 04, 2013 6:40 pm

And there should be an if there...
Friendship is magic!

R.I.P. Tracy M. You will be missed.
User avatar
stranac
 
Posts: 1790
Joined: Thu Feb 07, 2013 3:42 pm

Re: Help with a while loop

Postby hootdoog » Wed Dec 04, 2013 6:45 pm

stranac wrote:And there should be an if there...


I have placed an if there, but now the equals is highlighted red ://

if enemy == <= 0:
hootdoog
 
Posts: 14
Joined: Wed Dec 04, 2013 5:22 pm

Re: Help with a while loop

Postby hootdoog » Wed Dec 04, 2013 6:45 pm

hootdoog wrote:
stranac wrote:And there should be an if there...


I have placed an if there, but now the equals is highlighted red ://

if enemy == <= 0:


The underlined equals is the one that is highlighted
hootdoog
 
Posts: 14
Joined: Wed Dec 04, 2013 5:22 pm

Re: Help with a while loop

Postby Yoriz » Wed Dec 04, 2013 6:48 pm

Code: Select all
if enemy == <= 0:

is not the same as
Code: Select all
if enemy <= 0:

An if can be used without using == , <= is an alternative to ==
Due to the reasons discussed here we will be moving to python-forum.io/ on October 1 2016
This forum will be locked down and no one will be able to post/edit/create threads, etc. here from thereafter. Please create an account at the new site to continue discussion.
User avatar
Yoriz
 
Posts: 1672
Joined: Fri Feb 08, 2013 1:35 am
Location: UK

Re: Help with a while loop

Postby hootdoog » Wed Dec 04, 2013 6:53 pm

Yoriz wrote:
Code: Select all
if enemy == <= 0:

is not the same as
Code: Select all
if enemy <= 0:

An if can be used without using == , <= is an alternative to ==


I just tested the program but it is not recognising the fact that the enemys health is under zero

Code: Select all
import random

rounds = 0
health = 100
enemy = 50
print (" I============================I ")
print (" I== Welcome To TurkeyQuest ==I ")
print (" I============================I ")
print ("")
print ("")
name = input("What is your name young turkey: ")
print ("")
print ("Thats a stupid name", name)
print ("")
weapon = input(" To your left you see a sword, to your right a bow what do you choose: ")
if weapon == "Sword":
    print ("")
    print ("Haha, swords suck!")
    print ("")
elif weapon == "Bow":
    print ("")
    print("Lol, Bows suck!")
    print ("")
elif weapon == "sword":
    print ("")
    print ("Haha, swords suck!")
    print ("")
elif weapon == "bow":
    print ("")
    print("Lol, Bows suck!")
else:
    print ("Choose a real weapon!")
print ("")
print("Look a Hopscotch bandit!")
attack = "Attack"
#attack = input("What do you! [Attack, Run]: ")
print ("")
#if attack == "Attack":
    #while enemy >1 or health >0 or attack == "run":  # the more than or less than is an indicator not a guideline
while attack == "Attack":
        enemy = enemy - random.randint(1,15)
        print ("You attack with your", weapon)
        print ("Enemy Health: ", enemy )
        print ("")
        health = health - random.randint(1,10) # WOO SPECIAL RANDOM THINGS YEAH WOO IT WORKS!!!!! (Note: Im really happy I got that to work!)
        print ("Enemy attacks")
        print ("Your Health: ", health )
        print ("")
        attack = input("What do you! [Attack, Run]: ")
        print ("")
        if enemy <= 0:
            break
            print ("you slay the enmy with grace!")
hootdoog
 
Posts: 14
Joined: Wed Dec 04, 2013 5:22 pm

Re: Help with a while loop

Postby Yoriz » Wed Dec 04, 2013 6:59 pm

You have your if statement that breaks out of the while loop after asking to Attack or Run.
Due to the reasons discussed here we will be moving to python-forum.io/ on October 1 2016
This forum will be locked down and no one will be able to post/edit/create threads, etc. here from thereafter. Please create an account at the new site to continue discussion.
User avatar
Yoriz
 
Posts: 1672
Joined: Fri Feb 08, 2013 1:35 am
Location: UK

Re: Help with a while loop

Postby hootdoog » Wed Dec 04, 2013 7:04 pm

Yoriz wrote:You have your if statement that breaks out of the while loop after asking to Attack or Run.


Oh I see! Thank you so much :) You have helped a lot, the perfect blend of somewhat telling and nudging in the right direction! :D

Thanks so much, Rhys!
hootdoog
 
Posts: 14
Joined: Wed Dec 04, 2013 5:22 pm


Return to General Coding Help

Who is online

Users browsing this forum: No registered users and 7 guests