如果语句不断返回false

时间:2017-08-16 18:15:13

标签: python python-2.7

即使我确切地输入2500,Cloud3中的if语句也会一直返回else分支。我不知道如何解决这个问题。我已经将2500的值赋值为square,然后将raw_input设置为answer2,所以如果answer2 ==它应该是真的并且执行第一个if分支。

另外,我复制了RealEngine和CloudEngine类,因为我无法理解它们。所以如果有人可以解释他们发生了什么,我将不胜感激。这整个脚本是一个基于文本的游戏。

from sys import exit
from random import randint

class Smaker(object):
    def enter(self):
        print "whatever"
        exit(1)
class RealEngine(object):
    def __init__(self,scene_map):
        self.scene_map=scene_map
    def start(self):
        current_scene=self.scene_map.opener()
        while True:
            print "\n--------------"
            upcomingscene=current_scene.enter()
            current_scene=self.scene_map.play(upcomingscene)

class Slyremarks(Smaker):
    slyremarks=["My mom is better at this than you.",
                "You Dame ningen",
                "Seriously, how lame can a guy get???",
                "I thought you were better than this.",
                "Cheryl is really gonna be in trouble for recruiting such a 
                noob",
                "*Stares in disbelief*",
                "HAHAHAHAHAHAHAH,LOSERRRRRRRR!",
                "You seriously thought you could beat this game???",
                "Man, I'm gonna make this a meme and send this to God to 
                brighten up his day"]

    def enter(self):
        print Slyremarks.slyremarks[randint(0,len(self.slyremarks)-1)]
        exit(1)


class Cloud1(Smaker):
    def enter(self):
        print "You are being sent to  heaven after you died of diabetes,"
        print  "you fat shit.You should be ashamed of yourself."
        print "Anyway your at the gates of heaven and the guy at the gate 
              asks"
        print "you a question.'what do you want,a hotel or a mansion?'"
        print "What do you want? The simplicity of a motel or a damn 
               mansion?"
        answer=raw_input(">")
        if answer=="mansion":
            print "DUUH.I thought so.You magically teleport to your mansion 
                   that is made of gold and"
            print "diamonds but wait!!God says you have to go 
                   complete the challenge of the 9 clouds"
            print "before you can live it up in paradise."
            print "You're already on cloud 1, now the trial begins in 
                   cloud2"
            return 'cloud2'
        elif answer=="motel":
            print "Your simplistic tastes are beyond me but because of your 
                   odd ,choice the giant"
            print "gatekeeper laughs so hard he makes you fall of the cloud 
                   and inflict permadeath on you"
            return 'marks'

class Cloud2(Smaker):
    def enter(self):
        print "God says:OKAY!For the first test I want you to do a backflip 
              or  frontflip"
        print "which do you want to do?"
        action=raw_input(">")
        if action=="backflip":
            print "You stick the landing like a boss but God,being a fickle 
                   being,decided that it"
            print "Was boring and damns you to spend eternity in a motel 
                   kitchen;serving others"
            return 'marks'
        elif action=="frontflip":
            print "You fail miserably and smash your front teeth on the 
                   pavement"
            print "Yes, heaven has pavement.But you made God laugh 
                   hysterically and "
            print "so he rewards you with passage to cloud 3"
            return 'cloud3'

class Cloud3(Smaker):
    def enter(self):
        square=2500
        self.square=square
        print "For this cloud you have to solve a math problem:"
        print "What is:the square of 50?"
        answer2=raw_input(">")
        if answer2==square:
            print "Correct!!"
            print "WOW you must be some sort of math wiz!!"
            return 'trap'
        else:
            print "Wrong.You die."
            print "Like have you ever even been to school???"
            return 'marks' 
class trapfall(Smaker):
    pass
class Cloud4(Smaker):
    pass
class Cloud4(Smaker):
    pass
class Cloud5(Smaker):
    pass
class Cloud6(Smaker):
    pass
class Cloud7(Smaker):
    pass
class Cloud8(Smaker):
    pass
class Cloud9(Smaker):
    pass
class CloudEngine(object):
    clouds=  {'cloud1':Cloud1(),
            'cloud2':Cloud2(),
            'cloud3':Cloud3(),
            'cloud4':Cloud4(),
            'cloud5':Cloud5(),
            'cloud6':Cloud6(),
            'cloud7':Cloud7(),
            'cloud8':Cloud8(),
            'trap':trapfall(),
            'cloud9':Cloud9(),
            'marks':Slyremarks()}
    def __init__(self,start_scene):
        self.start_scene=start_scene
    def play(self,scene):
        return CloudEngine.clouds.get(scene)
    def opener(self):
        return self.play(self.start_scene)
ooh=CloudEngine('cloud1')
asldkfj=RealEngine(ooh)
asldkfj.start()

2 个答案:

答案 0 :(得分:3)

raw_input会返回str类型,您需要将其转换为int

答案 1 :(得分:1)

Python 2.7.10 (default, Oct 23 2015, 19:19:21) 
[GCC 4.2.1 Compatible Apple LLVM 7.0.0 (clang-700.0.59.5)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> raw_input(">")
>2500
'2500'
>>> raw_input(">") == "2500"
>2500
True
>>> raw_input(">") == 2500
>2500
False
>>>