阵列无法正确排列 - python
好吧,我又回来了。我上周询问了我在数组中遇到的一些问题,并解决了问题,但我遇到了类似的问题。我朋友的代码看起来完全像我一样,就像没有开玩笑的格式几乎相同,但他的作品和我的作品却没有。除了显示正确的答案,然后用户输入的内容时,一切都起作用。如果这似乎是一种奇怪的方法,我知道这是我应该如何做的,不要因为它而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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
基本上,您将问题_input存储为一个不断变化的字符串,因此您最后试图在字符串“ A”中找到第20个字符。我创建了一个新数组,其中存储用户答案,然后在最后使用这些数组。
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.