如何将列表中的项目与用户输入进行比较?

时间:2017-06-12 14:14:34

标签: python list

我试图将用户的答案与列表进行比较:

answers=["Sundar Pichai","Mark Zukenberg","Narendra Modi"]
print ('Welcome to Quizz!!')
player_name= raw_input("Enter Your Name:")
def out_ans():
    guess_ans = raw_input("enter your answer:")    
    for ans in answers:
        if guess_ans.lower() == ans.lower():
            print(player_name  +" correct answer")

        else:
            print(player_name + " its a wrong answer")
print ("Q.No 1 Who is the present CEO of Google??")             
out_ans()
print ("Q.No 2 Who is the founder of FaceBook??")
out_ans()

这是输出

Welcome to Quizz!!

Enter Your Name: xyz
Q.No 1 Who is the present CEO of Google??
enter your answer: sundar pichai
xyz its a correct answer
xyz its a wrong answer
xyz its a wrong answer
Q.No 2 Who is the founder of FaceBook??
enter your answer: mark zukenberg
xyz its a wrong answer
xyz its a correct answer
xyz its a wrong answer

4 个答案:

答案 0 :(得分:4)

我认为您只需要打印一次消息,告知用户他的答案的正确性。

然后你需要这样的东西:

def out_ans():
    guess_ans = raw_input("enter your answer:")
    correct = False
    for ans in answers:
        if guess_ans.lower() == ans.lower():
            correct = True
    if correct:
        print(player_name  +" correct answer")
    else:
        print(player_name + " its a wrong answer")

答案 1 :(得分:2)

answers=["Sundar Pichai","Mark Zukenberg","Narendra Modi"]
print ('Welcome to Quizz!!')
player_name= raw_input("Enter Your Name:")

def out_ans():
    guess_ans = raw_input("enter your answer:")
    if guess_and.lower() in [x.lower() for x in answers]:
        print(player_name  +" correct answer")
    else:
        print(player_name + " its a wrong answer")


print ("Q.No 1 Who is the present CEO of Google??")             
out_ans()
print ("Q.No 2 Who is the founder of FaceBook??")
out_ans()

但是当我回答第一个问题'马克祖肯伯格'时,你认为会发生什么? :)是的,你是对的 - 程序会说这是正确答案。为了避免这种情况,也要更改测验的架构:

def check_ans(player_ans, valid_answer):
    result = "correct answer" if player_ans.lower() == valid_answer.lower() else "its a wrong answer"
    return '{0} {1}'.format(player_name, result)

quiz = (("Q.No 1 Who is the present CEO of Google??", "Sundar Pichai"),
        ("Q.No 2 Who is the founder of FaceBook??", "Mark Zukenberg"))

print('Welcome to Quizz!!')
player_name = input("Enter Your Name:")

for question, answer in quiz:
    player_ans = input(question)
    print(check_ans(player_ans, answer))

答案 2 :(得分:1)

您不必每次都打印,当您找到答案时,请返回:

def out_ans():
    guess_ans = raw_input("enter your answer:")    
    for ans in answers:
        if guess_ans.lower() == ans.lower():
            #if we find a correct answer, exit
            print(player_name  +" correct answer")
            return 
    #if didn't find any correct answer 
    print(player_name + " its a wrong answer")

答案 3 :(得分:1)

试试这个:

answers=[ "Sundar Pichai", "Mark Zukenberg", "Narendra Modi" ]
flagGuessWrong = False
print ( 'Welcome to Quizz!!' )

player_name = raw_input( "Enter Your Name:" )


def out_ans():
  guess_ans = raw_input( "enter your answer:" )    
  for ans in answers:
    if guess_ans.upper() == ans.upper():
      print(player_name  +" correct answer")
      flagGuessWrong = False
      break
    else:
      flagGuessWrong = True

  if(flagGuessWrong):
      print(player_name + " its a wrong answer")


print ("Q.No 1 Who is the present CEO of Google??")             
out_ans()
print ("Q.No 2 Who is the founder of FaceBook??")
out_ans()

这样它会比较你在阵列上的所有内容,但请记住它不会给你正确的答案(例如:Q.No 2谁是FaceBook的创始人?? => Narendra Modi(将给出这是因为你循环遍历整个数组,一旦找到匹配的对,它就会离开(我的修改)。其余的你需要应用一些逻辑,也许是一个字典,这样他就能找到键/值对并返回正确的答案。