阵列无法正确排列 - python

发布于 2025-01-22 21:27:04 字数 2426 浏览 0 评论 0原文

好吧,我又回来了。我上周询问了我在数组中遇到的一些问题,并解决了问题,但我遇到了类似的问题。我朋友的代码看起来完全像我一样,就像没有开玩笑的格式几乎相同,但他的作品和我的作品却没有。除了显示正确的答案,然后用户输入的内容时,一切都起作用。如果这似乎是一种奇怪的方法,我知道这是我应该如何做的,不要因为它而badge to,哈哈。我会收到索引错误,但我不确定为什么。它必须像上周一样简单而类似的东西。

def main():

    # Initializes answer sheet
    questions_answers = ['B', 'D', 'A', 'A', 'C', "A", 'B', 'A', 'C', 'D', 'B', 'C', 'D', 'A', 'D', 'C', 'C', 'B', 'D', 'A', ]
    # Initializes the array for the questions the user got wrong
    questions_wrong = []
    # Initializes the amount correct variable
    amount_correct = 0
    # Initializes the amount incorrect variable
    amount_incorrect = 0

    # Starts a for loop to get users answers
    for question_number in range(20):
        # Asks user to input their answer
        questions_input = input('Enter Your answer for Question #' + str(question_number + 1) + ': ')
        # Sets inputs to uppercase
        questions_input = questions_input.upper()

        # If statement to determine if input is right
        if questions_input == questions_answers[question_number]:
            amount_correct += 1
        # Else statement to add 1 to incorrect count and the answer to the list of questions wrong
        else:
            questions_wrong.append((question_number + 1, questions_input))
            amount_incorrect += 1

    # Prints blank line
    print()

    # If statement to determine pass
    if amount_correct >= 15:
        # Prints thta the user passed
        print('**YOU PASSED**')
    # Else statement to determine fail
    else:
        # Prints the user failed
        print('**YOU FAILED**')

    # Prints the number correct
    print('Number correct: ', amount_correct)

    # Pirnts the number incorrect
    print('Number incorrect:', amount_incorrect)
    # Prints 3 blank lines and headers for comparing wrong answers
    print()
    print()
    print()
    print('You got the following questions wrong:')
    print()
    print('Question     Correct     Your Answer')
    print('--------     -------     -----------')
    # Runs a for loop to display what the user got wrong and what the right answer was
    for question_number in range(20):
        if questions_answers[question_number] != questions_input[question_number]:
            print('  ', question_number + 1, '         ', questions_answers[question_number], '           ', questions_input[question_number])

# End of main procedure
main()

Okay I'm back again. I asked last week about some issues I was having with arrays and I fixed the problem but I'm running into a similar problem. My friend's code looks EXACTLY like mine like no joke almost identical in format but his works and mine does not. Everything works except for the very end when it shows the right answer and then what the user entered. If this seems like a weird way to do this, I know its how I'm supposed to, don't badger me for it haha. I get an indexing error but I'm not sure why. It must be something simple and similar like last week.

def main():

    # Initializes answer sheet
    questions_answers = ['B', 'D', 'A', 'A', 'C', "A", 'B', 'A', 'C', 'D', 'B', 'C', 'D', 'A', 'D', 'C', 'C', 'B', 'D', 'A', ]
    # Initializes the array for the questions the user got wrong
    questions_wrong = []
    # Initializes the amount correct variable
    amount_correct = 0
    # Initializes the amount incorrect variable
    amount_incorrect = 0

    # Starts a for loop to get users answers
    for question_number in range(20):
        # Asks user to input their answer
        questions_input = input('Enter Your answer for Question #' + str(question_number + 1) + ': ')
        # Sets inputs to uppercase
        questions_input = questions_input.upper()

        # If statement to determine if input is right
        if questions_input == questions_answers[question_number]:
            amount_correct += 1
        # Else statement to add 1 to incorrect count and the answer to the list of questions wrong
        else:
            questions_wrong.append((question_number + 1, questions_input))
            amount_incorrect += 1

    # Prints blank line
    print()

    # If statement to determine pass
    if amount_correct >= 15:
        # Prints thta the user passed
        print('**YOU PASSED**')
    # Else statement to determine fail
    else:
        # Prints the user failed
        print('**YOU FAILED**')

    # Prints the number correct
    print('Number correct: ', amount_correct)

    # Pirnts the number incorrect
    print('Number incorrect:', amount_incorrect)
    # Prints 3 blank lines and headers for comparing wrong answers
    print()
    print()
    print()
    print('You got the following questions wrong:')
    print()
    print('Question     Correct     Your Answer')
    print('--------     -------     -----------')
    # Runs a for loop to display what the user got wrong and what the right answer was
    for question_number in range(20):
        if questions_answers[question_number] != questions_input[question_number]:
            print('  ', question_number + 1, '         ', questions_answers[question_number], '           ', questions_input[question_number])

# End of main procedure
main()

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

夏夜暖风 2025-01-29 21:27:04

基本上,您将问题_input存储为一个不断变化的字符串,因此您最后试图在字符串“ A”中找到第20个字符。我创建了一个新数组,其中存储用户答案,然后在最后使用这些数组。

# Initializes answer sheet
questions_answers = ['B', 'D', 'A', 'A', 'C', "A", 'B', 'A', 'C', 'D', 'B', 'C', 'D', 'A', 'D', 'C', 'C', 'B', 'D', 'A', ]
# Initializes the array to store the answers the User enters 
user_answers = []
# Initializes the array for the questions the user got wrong
questions_wrong = []
# Initializes the amount correct variable
amount_correct = 0
# Initializes the amount incorrect variable
amount_incorrect = 0

# Starts a for loop to get users answers
for question_number in range(20):
    # Asks user to input their answer
    questions_input = input('Enter Your answer for Question #' + str(question_number + 1) + ': ')
    # Sets inputs to uppercase
    questions_input = questions_input.upper()
    user_answers.append(questions_input)

    # If statement to determine if input is right
    if questions_input == questions_answers[question_number]:
        amount_correct += 1
    # Else statement to add 1 to incorrect count and the answer to the list of questions wrong
    else:
        questions_wrong.append((question_number + 1, questions_input))
        amount_incorrect += 1

# Prints blank line
print()

# If statement to determine pass
if amount_correct >= 15:
    # Prints thta the user passed
    print('**YOU PASSED**')
# Else statement to determine fail
else:
    # Prints the user failed
    print('**YOU FAILED**')

# Prints the number correct
print('Number correct: ', amount_correct)

# Pirnts the number incorrect
print('Number incorrect:', amount_incorrect)
# Prints 3 blank lines and headers for comparing wrong answers
print()
print()
print()
print('You got the following questions wrong:')
print()
print('Question     Correct     Your Answer')
print('--------     -------     -----------')
# Runs a for loop to display what the user got wrong and what the right answer was
for question_number in range(20):
    if questions_answers[question_number] != user_answers[question_number]:
        print('  ', question_number + 1, '         ', questions_answers[question_number], '           ', user_answers[question_number])

Basically you stored questions_input as a string that kept changing, so you at the end you were trying to find the 20th character in the string "A" for example. I created a new array where the users answers are stored and then those are used at the end.

# Initializes answer sheet
questions_answers = ['B', 'D', 'A', 'A', 'C', "A", 'B', 'A', 'C', 'D', 'B', 'C', 'D', 'A', 'D', 'C', 'C', 'B', 'D', 'A', ]
# Initializes the array to store the answers the User enters 
user_answers = []
# Initializes the array for the questions the user got wrong
questions_wrong = []
# Initializes the amount correct variable
amount_correct = 0
# Initializes the amount incorrect variable
amount_incorrect = 0

# Starts a for loop to get users answers
for question_number in range(20):
    # Asks user to input their answer
    questions_input = input('Enter Your answer for Question #' + str(question_number + 1) + ': ')
    # Sets inputs to uppercase
    questions_input = questions_input.upper()
    user_answers.append(questions_input)

    # If statement to determine if input is right
    if questions_input == questions_answers[question_number]:
        amount_correct += 1
    # Else statement to add 1 to incorrect count and the answer to the list of questions wrong
    else:
        questions_wrong.append((question_number + 1, questions_input))
        amount_incorrect += 1

# Prints blank line
print()

# If statement to determine pass
if amount_correct >= 15:
    # Prints thta the user passed
    print('**YOU PASSED**')
# Else statement to determine fail
else:
    # Prints the user failed
    print('**YOU FAILED**')

# Prints the number correct
print('Number correct: ', amount_correct)

# Pirnts the number incorrect
print('Number incorrect:', amount_incorrect)
# Prints 3 blank lines and headers for comparing wrong answers
print()
print()
print()
print('You got the following questions wrong:')
print()
print('Question     Correct     Your Answer')
print('--------     -------     -----------')
# Runs a for loop to display what the user got wrong and what the right answer was
for question_number in range(20):
    if questions_answers[question_number] != user_answers[question_number]:
        print('  ', question_number + 1, '         ', questions_answers[question_number], '           ', user_answers[question_number])
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文