2017-08-16 88 views
0

的如果Cloud3聲明保留返回else分支,即使我進入2500 exactly.I不知道如何解決this.I已分配的2500值方,然後設置要的raw_input ANSWER2因此,如果ANSWER2 ==廣場,它應該是真實和執行的第一個分支,如果。如果聲明保持返回false

而且,我複製了RealEngine和CloudEngine類,因爲我無法理解他們。所以,我將不勝感激,如果有人可以解釋什麼是怎麼回事them.This整個腳本是一個基於文本的遊戲。

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() 
+9

的raw_input'()'返回一個字符串,這是不一樣的一個int – TemporalWolf

+0

我也建議你不要使用變量名,如'asldkfj' – TemporalWolf

回答

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 
>>> 
3

raw_input返回str類型,則需要將其轉換爲int