我有一个python函数来重置脚本';跑什么?
所以当我在IDE中运行它时,终端弹出,我输入任何东西,它超出了预期的方式,但随后终端关闭。是否有一个功能可以简单地用相同的代码重置终端中的功能,以便简单地输入类似游戏的内容我有一个python函数来重置脚本';跑什么?,python,string,integer,reset,Python,String,Integer,Reset,所以当我在IDE中运行它时,终端弹出,我输入任何东西,它超出了预期的方式,但随后终端关闭。是否有一个功能可以简单地用相同的代码重置终端中的功能,以便简单地输入类似游戏的内容 另外,我还可以得到“将所有内容都放在一个函数中,在完成脚本后,您可以添加一个Yes或No问题,如果答案是Yes,您只需再次调用该函数 import sys def myScript(): # Rate our Love!! ### Press F5 ## then input a rat
另外,我还可以得到“将所有内容都放在一个函数中,在完成脚本后,您可以添加一个Yes或No问题,如果答案是Yes,您只需再次调用该函数
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()
导入系统
def myScript():
#评价我们的爱!!
###按F5
##然后输入到目前为止我们关系的评分
打印“为我们的关系输入a评级”
love_rate=原始输入(“为我们的关系键入a评级:”)
###单词答案
idk='idk'
否='否'
是=‘是’
lol='lol'
smh='smh'
爱(爱的比率)
def love(n):
如果n<0:
打印“为什么会是负片?!”?!"
elif n==“是”:
打印“如果是这样的话,我想我们会没事的。”
elif n==“否”:
打印“那么……这很尴尬”
elif n=='lol':
打印“这甚至不是一个数字……嗅嗅”
elif n==“smh”:
打印“我有点疯了这是你想到的答案”
##编号条目
elif n==“0”:
打印“*气喘吁吁*你不配参加这个节目”
elif n==“1”:
打印“哇,这有点伤人,不会说谎”
elif n==“2”:
打印“你每天至少让我微笑一次”
elif n==“3”:
打印“你不会相信让这个程序正常运行是多么烦人!“+”这就是我能得到的一切
elif n==“4”:
打印“让我们”+“再高一点”
elif n==“5”:
打印“你令人难忘,这就是你”
elif n==“6”:
打印“永远拥有,永远”,“暗示”*填空
elif n==“7”:
打印“我永远不会离开你,我太爱你了”
elif n==“8”:
打印“8/10仍然只是个B,也许我还不够努力”
elif n==“9”:
打印“嗯,我还不是完美的,我可能已经看到了。我想我必须继续努力:)”
elif n==“10”:
打印“我认为你在夸大其词,我真的还没有那么好”
elif n==“11”:
打印“”“我有时可能有点不成熟,对此我很抱歉,我保证我会变得更好。但我需要你。我需要你帮我。你能做到吗
elif n==“12”:
打印“我认为刻度不应该这么高”
elif n==“13”:
打印“好的,现在你推它。”
elif n==“14”:
打印“好的,比例上升到10左右。停止“
elif n==“15”:
打印“再上一个号码”。我敢说你”
elif n==“16”:
打印“再上一个号码”。看看会发生什么”
elif n==“17”:
打印“另一个号码”
elif n==“18”:
打印“多一张”
elif n==“19”:
打印“停止”
elif n==“92412”:
打印“我认为这是一个极好的答案,迫不及待地等待我们的周年纪念”
其他:
打印“我仍然真的希望有一天我们能结婚。”
打印“”“要重试吗?”“
是=设置(['yes'、'y'、'ye'、'')
否=设置(['no','n']))
选项=原始输入()
如果选择“是”:
myScript()
elif选择否:
系统出口(0)
其他:
sys.stdout.write(“请回答“是”或“否”)
myScript()
myScript()
之所以n<0
不起作用,是因为raw\u input
返回一个字符串,而n<0
需要一个int才能起作用,因为你无法判断一个数字是否大于一个单词,而不是raw\u input
,因为input
将返回一个int,然后on你是elif语句,你可以去掉引号,使它们成为数字
你可能不需要那个冗长的解释,但不管怎样
你能做的就是
n = raw_input("Rate Relationship: ")
if '-' in n:
print ("Why would it be Negative!?")
将所有内容封装在函数中,并根据用户输入递归调用它们
示例-只需将这两个函数添加到程序中,而不是将所有逻辑都包含在一个大型函数中:
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()
有几种方法可以将字符串转换为数字以使其正常工作:
添加一个try
,除了将输入转换为整数的
块之外。这将尝试将输入转换为整数,如果不能,则保持原样。这意味着您必须更改elif
语句;因此它们比较整数,而不是整数和字符串
使用regex
检测是否存在负整数,并相应地进行转换
试试
,除外
示例:
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 :)"
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))
这不起作用。很遗憾,由于某些原因,它根本不起作用。没有看到还有其他功能,请重试。这应该可以起作用,但put uselove\u rate=raw\u input(“为我们的关系键入评级:”)
问题是,当我使用输入时,答案这个词不起作用。答案这个词并不是那么必要,但我真的很好奇是否有办法做到这一点。是的,我想你可以,但我真的不知道怎么做
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))