允许用户重试python中的功能

发布于 2025-02-02 11:08:56 字数 1041 浏览 3 评论 0原文

我的代码是一个测验 - 按照您最喜欢的“书本”中哪个字符的行 - 基本上是一系列问题,其中包括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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

撩动你心 2025-02-09 11:08:57

在循环和循环时使用,直到使用break退出循环的有效条件。另外,不需要像QUEST_ANS.ANS.ANS那样在函数上设置属性 - 请注意,ans本身就足够了。

def quest_ans():
    ans=0
    while True:
        answer=input(str.lower("Please Enter a,b,c,d or e: "))
        if answer.lower() == 'a':
            ans =2
            break
        elif answer.lower() == 'b':
            ans=3
            break
        elif answer.lower() == 'c':
            ans=4
            break
        elif answer.lower() == 'd':
            ans=5
            break
        elif answer.lower() == 'e':
            ans=6
            break
        else:
            print('Try again, bro..')

    return ans

ans = quest_ans()
print('Dude youy entered:', ans)

请注意,从技术上讲,您的代码可以通过使用dict对数字值的映射答案来改进或简化:

ans_map = {'a': 2, 'b': 3, 'c': 4, 'd': 5, 'e': 6}


def quest_ans():

    while True:
        answer = input("Please Enter a,b,c,d or e: ".lower()).lower()

        if answer in ans_map:
            return ans_map[answer]

        print('Try again, bro..')


ans = quest_ans()
print('Dude youy entered:', ans)

Use a while loop and loop until a valid condition, which uses break to exit out of the loop. Also, don't need to set a property on the function like quest_ans.ans - note that ans is sufficient by itself.

def quest_ans():
    ans=0
    while True:
        answer=input(str.lower("Please Enter a,b,c,d or e: "))
        if answer.lower() == 'a':
            ans =2
            break
        elif answer.lower() == 'b':
            ans=3
            break
        elif answer.lower() == 'c':
            ans=4
            break
        elif answer.lower() == 'd':
            ans=5
            break
        elif answer.lower() == 'e':
            ans=6
            break
        else:
            print('Try again, bro..')

    return ans

ans = quest_ans()
print('Dude youy entered:', ans)

Note that, technically your code could be improved or simplified through use of a dict mapping answer to numeric value:

ans_map = {'a': 2, 'b': 3, 'c': 4, 'd': 5, 'e': 6}


def quest_ans():

    while True:
        answer = input("Please Enter a,b,c,d or e: ".lower()).lower()

        if answer in ans_map:
            return ans_map[answer]

        print('Try again, bro..')


ans = quest_ans()
print('Dude youy entered:', ans)
℡寂寞咖啡 2025-02-09 11:08:57

尝试以下操作:

def quest_ans():
    quest_ans.ans= 0 #just “ans” isn’t actually used
    while True: #to try over and over until a valid response is given
        
        answer=input("Please Enter a,b,c,d or e: ").lower()
        try: #if a 2+ character response is given, this will stop ord() causing an error
            if ord(answer) > 96 and ord(answer) < 102: #comparing ASCII of letter
                break
            else:
                print("please try again")
        except:
            print("please try again")

    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

Try this:

def quest_ans():
    quest_ans.ans= 0 #just “ans” isn’t actually used
    while True: #to try over and over until a valid response is given
        
        answer=input("Please Enter a,b,c,d or e: ").lower()
        try: #if a 2+ character response is given, this will stop ord() causing an error
            if ord(answer) > 96 and ord(answer) < 102: #comparing ASCII of letter
                break
            else:
                print("please try again")
        except:
            print("please try again")

    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
耀眼的星火 2025-02-09 11:08:57

我通常不使用递归,但我认为这是使用它的好时机。

您的编辑功能:

def quest_ans():
    ans = 0
    answer = input(str.lower("Please Enter a,b,c,d or e: "))
    if answer.lower() == 'a':
        ans = 2
    elif answer.lower() == 'b':
        ans = 3
    elif answer.lower() == 'c':
        ans = 4
    elif answer.lower() == 'd':
        ans = 5
    elif answer.lower() == 'e':
        ans = 6
    else:
        print("Invalid option entered. Try again...")
        ans = quest_ans()
    return ans


answer = quest_ans()

基本上,如果用户输入AE以外的其他内容,则其他块将捕获并再次调用该功能,如果用户不输入AE,则将继续重复。

希望这有帮助!

I usually don't use recursion, but I think this is a relatively good time to use it.

Your edited function:

def quest_ans():
    ans = 0
    answer = input(str.lower("Please Enter a,b,c,d or e: "))
    if answer.lower() == 'a':
        ans = 2
    elif answer.lower() == 'b':
        ans = 3
    elif answer.lower() == 'c':
        ans = 4
    elif answer.lower() == 'd':
        ans = 5
    elif answer.lower() == 'e':
        ans = 6
    else:
        print("Invalid option entered. Try again...")
        ans = quest_ans()
    return ans


answer = quest_ans()

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!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文