Python代码压缩
我是一个新手。我需要下面的帮助Python代码压缩,python,function,Python,Function,我是一个新手。我需要下面的帮助play_game()。我需要把它排到18行。我想在此代码中调用if和else函数,将其缩短那么多行 def play_game(): # def the plag game function which is the main control of the game level = get_level() quiz = game_data[level]['quiz'] print quiz answers_list = game
play_game()
。我需要把它排到18行。我想在此代码中调用if
和else
函数,将其缩短那么多行
def play_game(): # def the plag game function which is the main control of the game
level = get_level()
quiz = game_data[level]['quiz']
print quiz
answers_list = game_data[level]['answers']
blanks_index = 0
answers_index = 0
guesses = 3
while blanks_index < len(blanks):
user_answer = raw_input("So what's your answer to question " + blanks[blanks_index] + "? : ") #while, if and else to increment the blanks, answers, and guesses
if check_answer(user_answer,answers_list,answers_index) == "right_answer":
print "\n Lucky Guess!\n"
quiz = quiz.replace(blanks[blanks_index], user_answer.upper()) #prints appropriate responses
blanks_index += 1
answers_index += 1
guesses = 3
print quiz
if blanks_index == len(blanks):
return you_win()
else:
guesses -= 1
if guesses == 0:
return you_lost()
break
print "Incorrect. Try again only " + str (guesses) + " guesses left!"
play_game()
def play_game():#定义plag game函数,该函数是游戏的主控件
级别=获取级别()
测验=游戏数据[等级][“测验”]
打印测验
答案列表=游戏数据[等级][“答案”]
空白指数=0
答案(指数=0)
猜测=3
而空白指数
以下是play\u game()
子例程,减少到18行代码:
def play_game():
data = game_data[get_level()]
quiz, answers = data['quiz'], data['answers']
index, guesses = 0, 3
print quiz
while index < len(blanks):
user_answer = raw_input("So what's your answer to question " + blanks[index] + "? : ")
if check_answer(user_answer, answers, index) == "right_answer":
quiz = quiz.replace(blanks[index], user_answer.upper())
print "\nLucky Guess!\n\n" + quiz
guesses = 3
index += 1
else:
guesses -= 1
if guesses == 0:
return you_lost()
print "Incorrect. Try again only " + str(guesses) + " guesses left!"
return you_win()
def play_game():
数据=游戏数据[获取等级()
测验,答案=数据['quick'],数据['answers']
索引,猜测=0,3
打印测验
而索引
在无法实际运行代码的情况下,很难做到这一点。主要是代码清理。这不是这个问题的正确位置。试着在代码审查上发布。嗯。。。18行?为什么是18岁?我正在学习一门课程,这是我的反馈。代码的工作方式就像我想要的一样,只是不符合我的评论者的规范