gpt4 book ai didi

python - 在 Python 中调试问答游戏

转载 作者:行者123 更新时间:2023-12-01 06:10:46 25 4
gpt4 key购买 nike

创建打印问题的函数,将选项添加到列表中。

def print_et_list ():
answer_list = []
function = open ("modStory.txt","r")
#Question
question = function.readline()
print question
#Choices
one = answer_list.append (function.readline())
two = answer_list.append (function.readline())
for item in answer_list:
print item
#Solution
try:
solution = int(function.readline())
except:
print "There's an error in the answer"

##for the blank line
function.readline()


return question, one, two, solution, function

##Function for prompting the user for an answer, comparing an answer, keeping score and printing score.
def hey_user (solution):
score = 0
user_answer = int(raw_input ("Enter what you think the answer is, user.\n"))
if user_answer == solution:
print "You've got it right!"
score = score + 1
elif user_answer == 0:
sys.exit()
else:
print "You've got it wrong."
return score

def main ():
question, one, two, solution, function = print_et_list()
scoresofar = hey_user (solution)
print "\nYour score is now", scoresofar
while question:
question, one, two, solution, function = print_et_list()
function.close()

main ()


raw_input ("Hit enter to exit.")

出于某种原因,我无法让这个东西正确循环。上面的代码无限循环。以下是有问题的文本文件,只是乱码歌词。程序将正确运行第一个片段,并在用户给出答案后无限循环第一个片段。

Can you carry my drink I have everything else
1 - I can tie my tie all by myself
2 - I'm getting tired, I'm forgetting why
2

is diving diving diving diving off the balcony
1 - Tired and wired we ruin too easy
2 - sleep in our clothes and wait for winter to leave
1

While it sings to itself or whatever it does
1 - when it sings to itself of its long lost loves
2 - I'm getting tired, I'm forgetting why
2

最佳答案

要纠正无限循环,请避免在每次调用print_et_list()时重新打开文件

试试这个(我将function重命名为file_handle,以便在阅读代码时更加明确)

import sys

def print_et_list (file_handle):
answer_list = []
#Question
question = file_handle.readline()
print question
#Choices
one = file_handle.readline()
two = file_handle.readline()
answer_list.append(one)
answer_list.append (two)
for item in answer_list:
print item
#Solution
solution = None
try:
result = file_handle.readline()
result.replace("\n","")
solution = int(result)
except:
print "There's an error in the answer"

##for the blank line
file_handle.readline()
return question, one, two, solution

##file_handle for prompting the user for an answer, comparing an answer, keeping score and printing score.
def hey_user (solution, score=0):
user_answer = int(raw_input ("Enter what you think the answer is, user.\n"))
print "you answered '%s'"%user_answer
if user_answer == solution:
print "You've got it right!"
score += 1
elif user_answer == 0:
sys.exit()
else:
print "You've got it wrong."
return score

def main ():
file_handle = open ("modStory.txt","r")
question, one, two, solution = print_et_list(file_handle)
scoresofar = hey_user(solution)
print "\nYour score is now", scoresofar
while question:
question, one, two, solution = print_et_list(file_handle)
if question:
scoresofar = hey_user(solution, scoresofar)
print "\nYour score is now", scoresofar
file_handle.close()

main ()


raw_input ("Hit enter to exit.")

这不是一个完美的版本,但它似乎可以工作;)

关于python - 在 Python 中调试问答游戏,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6112162/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com