Member Avatar for HTMLperson5

Hi guys,

I am having a problem with the following code:

#Health set here
def win():
    import time
    print "You win!"
    print "Thanks for playing"
    time.sleep(10)
    quit()
def lose():
    import time
    print "You lose!"
    print "Thanks for playing"
    time.sleep(10)
    quit()
from random import randint
pos_damage = randint(15,472)
en_health = 1000
pl_health = 1000
#Define Caves
cave_p1 = "This part of the cave seems quite, strange, it is rather dark too."
cave_p2 = "There is not much here, quite....blank."
cave_p3 = "Rocky area, i'm afraid you might not make it without something"
#Use modules
import os, random
from time import sleep
#Menu
name = raw_input('State your name  >    ')
print "Welcome,", name, "\n[a] Start\n[b] Quit"
usr_chce = raw_input("> ")
if usr_chce == "a" or "A":
    #Game starts *here*
    print "You stand on an island, which cave do you go to ?\nType 'help' for a list of commands"
    usr_choice = raw_input('>')
    if usr_choice == "help":
        print "(1) Type Attack to attack"
        print "(2) Type Skip to skip attack\n(3) Type explore to explore caves"
    if usr_choice == "fight":
        for fight in range (9000):
            print "Enemies Health:", en_health
            print name, "Health:", pl_health
            pl_move = raw_input('FIGHT>>')
            if pl_move == "attack" or "Attack" or "ATTACK":
                print "You attack!"
                #Players move
                en_health = en_health - pos_damage
                if en_health < 1:
                    win()
                    break;
                #Computers move
                comps_choices = ["true", "false"]
                comps_choice = random.choice(comps_choices)
                if comps_choice == "true":
                    print "Computer attacks!"
                    pl_health = pl_health - pos_damage
                    if pl_health < 1:
                        lose()
                if comps_choice == "false":
                    print "Computer skips!"


            if pl_move == "skip" or "Skip" or "SKIP":
                print "You skip!"
                if pl_health < 1:
                    lose()
                    break;
            if pl_move == "":
                print "Invalid Move. Lose 100 life."
                pl_health = pl_health - 100
            else:
                print "INVALID MOVE. LOSE 100 LIFE"
                pl_health = pl_health - 100

(I know putting the "fight" part into a loop was stupid; but it was for experimental purposes)

State your name  >    fnewfewfwewfewfewfwnaiv!osgAy
Welcome, fnewfewfwewfewfewfwnaiv!osgAy 
[a] Start
[b] Quit
> a
You stand on an island, which cave do you go to ?
Type 'help' for a list of commands
>fight
Enemies Health: 1000
fnewfewfwewfewfewfwnaiv!osgAy Health: 1000
FIGHT>>attack
You attack!
Computer skips!
You skip!
INVALID MOVE. LOSE 100 LIFE
Enemies Health: 633
fnewfewfwewfewfewfwnaiv!osgAy Health: 900
FIGHT>>attack
You attack!
Computer skips!
You skip!
INVALID MOVE. LOSE 100 LIFE
Enemies Health: 266
fnewfewfwewfewfewfwnaiv!osgAy Health: 800
FIGHT>>
You attack!
You win!
Thanks for playing - COPYRIGHT NAVID MOMTAHEN

See the problem? If the player enters random stuff, or a blank line, it by default attacks or skips...Why?

Recommended Answers

All 3 Replies

line 60 condition is always True as or "some not empty string" is never False value

if pl_move == "skip" or "Skip" or "SKIP":

As pyTony said, the if statement will be always true. You'll need to do it like this:

if pl_move == "skip" or pl_move=="Skip" or pl_move=="SKIP":

the condition impose that you should check the equality for every choice of pl_move. As for if pl_move=="skip" or "Skip" here Python will interpret the "Skip" as a non-empty string, so a valid string, which results in a True answer, and by or, if the first condition isn't fulfilled, the second, which is the string will be considered "True".

Member Avatar for HTMLperson5

Its still behaving a little weirdly, but improved :)

Enemies Health: 1000
navid Health: 1000
FIGHT>>
You attack! (I didn't even say anything!) 
Computer attacks!
Invalid Move. Lose 100 life.
Enemies Health: 588
navid Health: 488
FIGHT>>skip
You attack! (I said skip!)
Computer skips!
You skip!
INVALID MOVE. LOSE 100 LIFE
Enemies Health: 176
navid Health: 388
FIGHT>>Skip
You attack! (What? I said Skip!)
You win!
Thanks for playing

The problems I have found are next to the output in brackets.
I have modified line 60 to your suggestion.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.