允许用户重试python中的功能
我的代码是一个测验 - 按照您最喜欢的“书本”中哪个字符的行 - 基本上是一系列问题,其中包括a,b,c,d,e作为答案。
`#Question 1
print("This is the text of question 1")
print("a - This is the text of answer a")
print("b - This is the text of answer b")
print("c - This is the text of answer c")
print("d - This is the text of answer d")
print("e - This is the text of answer e")
quest_ans()
answers.append(quest_ans.ans)`
代码下一步调用一个函数以允许用户输入答案,该函数还将其答案转换为一个数字,该数字将通过并将其附加到列表中,以便在最后。
这就是函数quest_ans()
def quest_ans():
ans=0
answer=input(str.lower("Please Enter a,b,c,d or e: "))
if answer.lower() == 'a':
quest_ans.ans =2
elif answer.lower() == 'b':
quest_ans.ans=3
elif answer.lower() == 'c':
quest_ans.ans=4
elif answer.lower() == 'd':
quest_ans.ans=5
elif answer.lower() == 'e':
quest_ans.ans=6
return quest_ans.ans
只要用户输入正确的答案选项之一, - 这一切都很好,但是我想通过打印A,B,C,C,D,E以外的其他内容来管理消息以提醒他们并重新调用该功能以允许他们输入正确的答案。 我尝试了尝试以外的块,而循环和我无法正确的语法。
任何帮助将不胜感激。 阿比
My code is a quiz - along the lines of which character in 'The book' are you most like - basically a series of questions with options of a,b,c,d,e as the answers.
`#Question 1
print("This is the text of question 1")
print("a - This is the text of answer a")
print("b - This is the text of answer b")
print("c - This is the text of answer c")
print("d - This is the text of answer d")
print("e - This is the text of answer e")
quest_ans()
answers.append(quest_ans.ans)`
The code next calls a function to allow the user input their answer, the function also takes that answer and converts it to a number, which is passed and appened to a list, for summing at the end.
This is the function quest_ans()
def quest_ans():
ans=0
answer=input(str.lower("Please Enter a,b,c,d or e: "))
if answer.lower() == 'a':
quest_ans.ans =2
elif answer.lower() == 'b':
quest_ans.ans=3
elif answer.lower() == 'c':
quest_ans.ans=4
elif answer.lower() == 'd':
quest_ans.ans=5
elif answer.lower() == 'e':
quest_ans.ans=6
return quest_ans.ans
As long as the user enters the one of the correct answer options - this all works fine, but I want to manage if the user enters something other than a, b, c, d, e by printing a message to alert them and re calling the function to allow them enter a correct answer.
I've tried try except blocks and while loops and I can't get the syntax right.
Any help would be appreciated.
Abbie
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
在循环和循环时使用
,直到使用
break
退出循环的有效条件。另外,不需要像QUEST_ANS.ANS.ANS
那样在函数上设置属性 - 请注意,ans
本身就足够了。请注意,从技术上讲,您的代码可以通过使用
dict
对数字值的映射答案来改进或简化:Use a
while
loop and loop until a valid condition, which usesbreak
to exit out of the loop. Also, don't need to set a property on the function likequest_ans.ans
- note thatans
is sufficient by itself.Note that, technically your code could be improved or simplified through use of a
dict
mapping answer to numeric value:尝试以下操作:
Try this:
我通常不使用递归,但我认为这是使用它的好时机。
您的编辑功能:
基本上,如果用户输入AE以外的其他内容,则其他块将捕获并再次调用该功能,如果用户不输入AE,则将继续重复。
希望这有帮助!
I usually don't use recursion, but I think this is a relatively good time to use it.
Your edited function:
Basically, if the user enters something other than a-e, the else block will catch it and call the function again, which will keep on being repeated if the user does not enter a-e.
Hope this helped!