[英]Getting Python to read the next line in a text file after user inputs Y to continue
我是一名学生,一直在寻找但没有找到对我有意义的答案,所以我道歉。
基本上我正在创建一个聊天机器人,它将从文本文件中读出事实。 我希望机器人阅读一行,然后要求继续。 如果是 Y,它会读出下一行直到结束或用户说 N。
到目前为止,它可以读取一行,然后它只是一遍又一遍地读取同一行。 我知道它也有其他问题,但我只是想让这部分在 atm 上工作。 我是自学的,如果这很糟糕,我很抱歉。
感谢任何输入!
# Defining functions
def well_bot():
print("Hi there, my name is Well Bot. ")
print("My job is to tell people general facts about mental health and different types of mental illnesses. ")
info = get_info()
print(info)
#this should only print if they want a fact
getfact = print_facts()
print(getfact)
#var to read from illnesses text file to add later
def get_info():
res = input("What can I teach you about today? \n[a] Facts \n[b] Types \n> ")
if res == "a":
print ("Okay. I will give you some general facts about mental health. ")
with open('facts.txt') as facts:
next_fact = facts.readline()
print (next_fact)
return continue_ask()
elif res == "b":
return "Okay, I can tell you about two different mental illnesses: \n[1] Depression \n[2] Bipolar Disorder \n> "
else:
print_message()
return get_info()
def continue_ask():
while True:
if input("Want to know more? Enter Y to continue or N to finish: ") == "Y":
with open('facts.txt') as facts:
next_fact = facts.readline()
print (next_fact)
return continue_ask()
else:
print ("Okay, thanks, bye! ")
break
def print_message():
print("I'm sorry, I didn't quite get that. Can you choose again? ")
return get_info()
#Call Bot
well_bot()
file = []
with open('facts.txt') as facts:
file = facts.read().split('\n')
line = 0
def continue_ask():
global line
while True: # not a fan of that would rather self call imo
if input("Want to know more? Enter Y to continue or N to finish: ") == "Y":
line+=1
try:
print(file[line])
except IndexError:
print("no more facts left :(")
break
else:
print ("Okay, thanks, bye! ")
break
这应该可以解决问题
def gen_lines(filepath):
"""Lines Generator: Open the file and yield each line"""
with open(filepath) as f:
for line in f:
yield line
def choose_argument():
"""Ask the user to choose an argument and return the right line generator."""
argument = input("What can I teach you about today? \n[a] Facts \n[b] Types \n> ")
if argument.lower() == "a":
return gen_lines("facts.txt")
elif argument.lower() == "b":
return gen_lines("types.txt")
else:
print("I'm sorry, I don't know what you want me to do.")
return choose_argument()
def asking_loop(line_generator):
"""Looping the lines asking the user at each step."""
# Get the first line from the generator.
print(next(line_generator))
# Loop until the user says "no".
while True:
whant_more = input("Do you want to know more? \n[y] Yes \n[n] No \n> ")
if whant_more.lower() == "y":
print(next(line_generator))
continue
elif whant_more.lower() == "n":
print("Okay, thanks, bye! ")
break
else:
print("I'm sorry, I don't know what you want me to do.")
continue
def main():
"""Main function."""
print("Hi there, my name is Well Bot. ")
print("My job is to tell people general facts about mental health and different types of mental illnesses. ")
line_generator = choose_argument()
asking_loop(line_generator)
if __name__ == "__main__":
main()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.