所以我正在尝试为学校开展项目,我想创建一个提示功能,允许我输入问题,提示类型,如果输入是"它将运行的功能。 Y",以及如果用户回复" N"它将输出的响应字符串。 这是我的代码:
#Will Steve Become a Millionaire?
#Interactive Fiction
#Zachary Williams
##Type 0 prompt = Y/N, Type 1 prompt = Int input.
def prompt(question, qType, yFunction, nResponse):
print(question)
answer=input()
if qType==0:
if answer=="Y" or answer=="y":
yFunction()
elif answer=="N" or answer=="n":
print(nResponse)
if answer=="X":
print("test")
else:
print("\"" + answer + "\" is not one of the options. Please type \"Y\" or \"N\".")
elif qType==1:
print("test")
else:
print("Error creating prompt.")
##Start the story.
def start_program():
print("It was 3:45 AM on a Saturday night. The only light in the room shone upon Steve's face, coming from the screen of his laptop, which he had been staring at for almost 12 hours straight. This was nothing new for Steve, but then an email popped up.")
prompt("\nWould you like to read the email? (Y/N)", 0, open_email(), "Steve closed the notification. It was time to go to bed.")
def opening():
print("Will Steve Become a Millionaire?")
prompt("Starting the program.", 0, start_program(), "Okay then.")
def open_email():
print("\nSteve opened the email.")
print("\n\"I am Prince Kufour Otumfuo,\" the email read, \"I am transferring $75,000,000 USD to the United States, and need your help to transport it into the country. If you help me do so, I will only have you transfer $65,000,000 to my account, leaving you with $10,000,000 to keep. If you would be so kind, please reply to this email.\"")
opening()
目前,这是我运行程序时返回的内容:
史蒂夫会成为百万富翁吗? 星期六晚上是凌晨3点45分。房间里唯一的灯光照在Steve的脸上,从他的笔记本电脑的屏幕上传来,他一直盯着他看了将近12个小时。这对史蒂夫来说并不是什么新鲜事,但随后又发了一封电子邮件。 史蒂夫打开电子邮件。"我是Kufour Otumfuo王子,"电子邮件已阅读,"我正在向美国转账75,000,000美元,需要您的帮助将其运送到该国。如果你帮我这么做,我只会把你的65,000,000美元转移到我的账户,剩下10,000,000美元。如果您愿意,请回复此电子邮件。"
您想看电子邮件吗? (Y / N)
在哪里说"史蒂夫打开电子邮件。"它应该问"你想看电子邮件吗? (是/否)"相反,它在打印出来后要求它。我对此为何非常困惑。对不起,如果我没有正确格式化,我以前从未在这里发布过。
谢谢!
答案 0 :(得分:2)
更改opening()
功能中的以下行
prompt("Starting the program.", 0, start_program(), "Okay then.")
要
prompt("Starting the program.", 0, start_program, "Okay then.")
请注意,我已删除()
。我认为你需要传递函数对象而不是调用函数。
答案 1 :(得分:0)
您需要在这里询问输入:
#Will Steve Become a Millionaire?
#Interactive Fiction
#Zachary Williams
##Type 0 prompt = Y/N, Type 1 prompt = Int input.
def prompt(question, qType, yFunction, nResponse):
print("y",question)
answer=input()
if qType==0:
if answer=="Y" or answer=="y":
yFunction()
elif answer=="N" or answer=="n":
print(nResponse)
if answer=="X":
print("test")
else:
print("\"" + answer + "\" is not one of the options. Please type \"Y\" or \"N\".")
elif qType==1:
print("test")
else:
print("Error creating prompt.")
##Start the story.
def start_program():
print("It was 3:45 AM on a Saturday night. The only light in the room shone upon Steve's face, coming from the screen of his laptop, which he had been staring at for almost 12 hours straight. This was nothing new for Steve, but then an email popped up.")
input_1=input("\nWould you like to read the email? (Y/N)")
if input_1 == "Y" or input_1 == "y":
open_email()
elif input_1 == "N" or input_1 == "n":
print('Okay then')
if input_1 == "X":
print("test")
def opening():
print("Will Steve Become a Millionaire?")
prompt("Starting the program.", 0, start_program(), "Okay then.")
def open_email():
print("\nSteve opened the email.")
print("\n\"I am Prince Kufour Otumfuo,\" the email read, \"I am transferring $75,000,000 USD to the United States, and need your help to transport it into the country. If you help me do so, I will only have you transfer $65,000,000 to my account, leaving you with $10,000,000 to keep. If you would be so kind, please reply to this email.\"")
opening()
或调用function
之类的函数,而不是function()
#Will Steve Become a Millionaire?
#Interactive Fiction
#Zachary Williams
##Type 0 prompt = Y/N, Type 1 prompt = Int input.
def prompt(question, qType, yFunction, nResponse):
print("y",question)
answer=input()
if qType==0:
if answer=="Y" or answer=="y":
yFunction()
elif answer=="N" or answer=="n":
print(nResponse)
if answer=="X":
print("test")
else:
print("\"" + answer + "\" is not one of the options. Please type \"Y\" or \"N\".")
elif qType==1:
print("test")
else:
print("Error creating prompt.")
##Start the story.
def start_program():
print("It was 3:45 AM on a Saturday night. The only light in the room shone upon Steve's face, coming from the screen of his laptop, which he had been staring at for almost 12 hours straight. This was nothing new for Steve, but then an email popped up.")
prompt("\nWould you like to read the email? (Y/N)", 0, open_email, "Steve closed the notification. It was time to go to bed.")
def opening():
print("Will Steve Become a Millionaire?")
prompt("Starting the program.", 0, start_program(), "Okay then.")
def open_email():
print("\nSteve opened the email.")
print("\n\"I am Prince Kufour Otumfuo,\" the email read, \"I am transferring $75,000,000 USD to the United States, and need your help to transport it into the country. If you help me do so, I will only have you transfer $65,000,000 to my account, leaving you with $10,000,000 to keep. If you would be so kind, please reply to this email.\"")
opening()