2013-05-22 36 views
1

所以当我在我的IDE中运行这个时,终端弹出,我输入任何东西,它超出了它的意图,但然后终端关闭。有没有一种功能可以简单地使用相同的代码重置终端中的功能,以便简单地继续输入像某种游戏之类的东西?我有一个python函数来重置脚本运行后?

而且反正是有,我得到了“< 0”的条件才能正常工作?我需要将字符串转换回数字才能正确执行。在功能

# Rate our Love!! 
### Press F5 
## then input a rating for our relationship so far 
print "Type a rating for our relationship" 
love_rate = raw_input() 
### word answers 
idk = 'idk' 
no = 'no' 
yes = 'yes' 
lol = 'lol' 
smh = 'smh' 
def love(n): 
    if n < 0 : 
     print "Why would it be negative?!" 
    elif n == 'yes' : 
     print " Well if that's the case, then I think we're gonna be just fine." 
    elif n == 'no' : 
     print 'well then... this is awkward' 
    elif n == 'lol' : 
     print '''THATS NOT EVEN A NUMBER 







     ......sniff''' 
    elif n == 'smh' : 
     print "I'm kinda mad that's an answer you thought of putting here"  
    ## numbered entries 
    elif n == '0' : 
     print " *gasps profusely* YOU DON'T DESERVE THIS PROGRAM" 
    elif n == '1' : 
     print "Wow that is kinda hurtful, not gonna lie" 
    elif n == '2' : 
     print "You make me smile at least once, each and every day" 
    elif n == '3' : 
     print"you wouldn't believe how annoying it was to get this program to run properly!" + " Thats all i get?" 
    elif n == '4' : 
     print "let's " + "shoot a little higher than that" 
    elif n == '5' : 
     print "you're unforgettable, that's what you are" 
    elif n == '6' : 
     print "always have, always '____' *hint* fill in the blank " 
    elif n == '7' : 
     print "i could never leave you, I love you too much" 
    elif n == '8' : 
     print "an 8/10 is still only a B, maybe I'm not trying hard enough" 
    elif n == '9' : 
     print " well, I'm not perfect yet, could have seen that one coming. Guess I just have to keep trying :)" 
    elif n == '10' : 
     print " i think you're exaggerating, i really am not that good yet"  
    elif n == '11' : 
     print """I can be a little immature sometimes and i'm sorry for that, i promise I can get better though. But i need you. I need you to help me out. Can you do that?""" 
    elif n == '12' : 
     print "I don't think the scale is supposed to go this high" 
    elif n == '13' : 
     print "alright now you're pushing it." 
    elif n == '14' : 
     print "alright, THE SCALE GOES UP TO AROUND 10. CEASE" 
    elif n == '15' : 
     print " go up one more number. I DARE YOU" 
    elif n == '16' : 
     print " go up one more number. see what happens" 
    elif n == '17' : 
     print "one more number" 
    elif n == '18' : 
     print "one more" 
    elif n == '19' : 
     print "STOP" 
    elif n == '92412' : 
     print " I think that is one fantastic answer, can't wait for our anniversary" 
    else: 
     print "I still really hope that we could get married someday." 
def reset_print(): 
    print """ 




Wanna Try Again? :D """ 
love(love_rate) 
reset_print() 

回答

0

把一切,你完成了剧本之后,你可以添加一个是或否的问题,如果答案是肯定的,你只是再次调用该函数。

import sys 
def myScript(): 
     # Rate our Love!! 
    ### Press F5 
    ## then input a rating for our relationship so far 
    print "Type a rating for our relationship" 
    love_rate = raw_input("Type a rating for our relationship:") 
    ### word answers 
    idk = 'idk' 
    no = 'no' 
    yes = 'yes' 
    lol = 'lol' 
    smh = 'smh' 
    love(love_rate) 

def love(n): 
     if n < 0 : 
      print "Why would it be negative?!" 
     elif n == 'yes' : 
      print " Well if that's the case, then I think we're gonna be just fine." 
     elif n == 'no' : 
      print 'well then... this is awkward' 
     elif n == 'lol' : 
      print '''THATS NOT EVEN A NUMBER ......sniff''' 
     elif n == 'smh' : 
      print "I'm kinda mad that's an answer you thought of putting here"  
     ## numbered entries 
     elif n == '0' : 
      print " *gasps profusely* YOU DON'T DESERVE THIS PROGRAM" 
     elif n == '1' : 
      print "Wow that is kinda hurtful, not gonna lie" 
     elif n == '2' : 
      print "You make me smile at least once, each and every day" 
     elif n == '3' : 
      print"you wouldn't believe how annoying it was to get this program to run properly!" + " Thats all i get?" 
     elif n == '4' : 
      print "let's " + "shoot a little higher than that" 
     elif n == '5' : 
      print "you're unforgettable, that's what you are" 
     elif n == '6' : 
      print "always have, always '____' *hint* fill in the blank " 
     elif n == '7' : 
      print "i could never leave you, I love you too much" 
     elif n == '8' : 
      print "an 8/10 is still only a B, maybe I'm not trying hard enough" 
     elif n == '9' : 
      print " well, I'm not perfect yet, could have seen that one coming. Guess I just have to keep trying :)" 
     elif n == '10' : 
      print " i think you're exaggerating, i really am not that good yet"  
     elif n == '11' : 
      print """I can be a little immature sometimes and i'm sorry for that, i promise I can get better though. But i need you. I need you to help me out. Can you do that?""" 
     elif n == '12' : 
      print "I don't think the scale is supposed to go this high" 
     elif n == '13' : 
      print "alright now you're pushing it." 
     elif n == '14' : 
      print "alright, THE SCALE GOES UP TO AROUND 10. CEASE" 
     elif n == '15' : 
      print " go up one more number. I DARE YOU" 
     elif n == '16' : 
      print " go up one more number. see what happens" 
     elif n == '17' : 
      print "one more number" 
     elif n == '18' : 
      print "one more" 
     elif n == '19' : 
      print "STOP" 
     elif n == '92412' : 
      print " I think that is one fantastic answer, can't wait for our anniversary" 
     else: 
      print "I still really hope that we could get married someday." 

     print """Want to try again?""" 
     yes = set(['yes','y', 'ye', '']) 
     no = set(['no','n']) 

     choice = raw_input().lower() 
     if choice in yes: 
      myScript() 
     elif choice in no: 
      sys.exit(0) 
     else: 
      sys.stdout.write("Please respond with 'yes' or 'no'") 
     myScript() 



myScript() 
+0

是没有遗憾的工作,留下它不工作由于某种原因 –

+0

没看不到还有其他功能,请再试一次。 –

+0

这应该工作,除了把使用'love_rate = raw_input(“为我们的关系类型的评级:”)' – Serial

1

的原因n < 0不会工作是因为raw_input给出了一个字符串返回和n < 0需要一个int的工作,因为你不能判断一个数是一个字做大做你想要使用input代替raw_input的原因是什么input会给你一个INT回来,然后在你的elif语句,使他们的数字你可以脱下引号

你可能didnt需要一个大长的解释,但无论

你可以做的是

n = raw_input("Rate Relationship: ") 

if '-' in n: 
    print ("Why would it be Negative!?") 
+0

与此问题是当我使用输入单词答案将无法正常工作。这不是像这个词的答案是必要的,但我真的好奇,如果有办法做到这一点 –

+0

好耶我猜你可以但我真的不知道如何 – Serial

+0

看看我的编辑 – Serial

1
  1. 裹一切功能,只是称他们为递归地依赖于用户输入。

示例 - 只是这两个功能添加到您的程序,而不是在一个巨大的函数的所有逻辑:

import sys 

def main(): 
    print "Type a rating for our relationship" 
    love_rate = raw_input() 
    love(love_rate) 
    try_again() 

def try_again()  
    print "Want to try again? [y]" 
      yes_list = ['yes','y', 'ye', '', yeah] 
      no_list = ['no','n'] 
      # Lower case it to normalise it 
      answer = raw_input().lower() 
      if answer in yes_list: 
       main() 
      elif answer in no_list: 
       sys.exit(0) 
      else: 
       sys.stdout.write("Please respond with 'yes' or 'no'") 
      try_again() 
  1. 有几个方法可以将字符串转换为数得到它的工作:

    1. 添加tryexcept块,你的输入转换为整数。这会尝试将输入转换为一个整数,并且如果不能的话就把它保留下来。这意味着你将不得不改变你的elif报表;所以他们比较整数,而不是一个整数和一个字符串。

    2. 使用一个regex来检测负整数的存在,并且相应地转换。

tryexcept例如:

try: 
    love_rate = int(love_rate) 
except ValueError: 
    pass 

def love(n): 
     if n < 0 : 
      print "Why would it be negative?!" 

     # .... 
     # Note the lack of '' 
     elif n == 9 : 
      print " well, I'm not perfect yet, could have seen that one coming. Guess I just have to keep trying :)" 

regex例如:

import re 

negative_integer_regex = re.compile(r'^(-\d+)$') 
matching_negative_integer = negative_integer_regex.match(love_rate) 
if matching_negative_integer: 
    love_rate = int(matching_negative_integer.groups(1)) 
相关问题