2017-10-12 5 views
0

私のコードの一部です。ユーザーが間違っていると言い続けます。各質問への答えは、リスト内で同じ順序です。Pythonの乱数に問題があります

質問銀行

 questions = ["45 - 20","32 - 12","54 + 41"] 

     # Answer Bank 
     answers = ["25","20","95"] 


     while qleft != 0: 


      # Get random question 
      qnumber = randint(0,2) 
      currentquestion = questions[qnumber] 
      currentanswer = answers[qnumber] 

      # Question 
      userchoice = int(input("What does {} equal? ".format(currentquestion))) 

      # Check Answer 
      if userchoice != currentanswer: 
       print("Incorrect") 
       qleft -= 1 
      elif userchoice == currentanswer: 
       print("Correct") 
       qleft -= 1 

      else: 
       print("That's not a valid answer") 
+0

整数 'の古典的なケースのように見える!=私にSTRING' ... – Shadow

答えて

1

は、ユーザーの答え、userchoiceは、整数であるが、実際の答えは、currentanswerは文字列です。

questions = ["45 - 20","32 - 12","54 + 41"] 

    # Answer Bank 
    answers = ["25","20","95"] 


    while qleft != 0: 


     # Get random question 
     qnumber = randint(0,2) 
     currentquestion = questions[qnumber] 
     currentanswer = answers[qnumber] 

     # Question 
     userchoice = input("What does {} equal? ".format(currentquestion)) 

     # Check Answer 
     if userchoice != currentanswer: 
      print("Incorrect") 
      qleft -= 1 
     elif userchoice == currentanswer: 
      print("Correct") 
      qleft -= 1 

     else: 
      print("That's not a valid answer") 
+0

他の誰かがそれを指摘するときに明らかに! –

2

あなたが等しいことはありませんstr答え、とint入力を比較している:あなたは単なる文字列として入力を残す場合は動作します。

あなたは、int型を扱う

answers = [25,20,95] 

answers = ["25","20","95"] 

を変更し、それがうまくいくにしたい場合。

また、input()の結果をintに変換しないでください。ここで

0

は差がある:!あなたはintに、ユーザの答えを変換しているので

import random as r 

    questions = ["45 - 20","32 - 12","54 + 41"] 

    # Answer Bank 
    answers = [25,20,95] 

    qleft = 3 

    while qleft != 0: 


     # Get random question 
     qnumber = r.randint(0,2) 
     currentquestion = questions[qnumber] 
     currentanswer = answers[qnumber] 

     # Question 
     userchoice = int(input("What does {} equal? ".format(currentquestion))) 

     # Check Answer 
     if userchoice != currentanswer: 
      print("Incorrect") 
      qleft -= 1 
     elif userchoice == currentanswer: 
      print("Correct") 
      qleft -= 1 

     else: 
      print("That's not a valid answer") 

予告 "25" という= 25

0

は、正しい答えはまた、int型であることを旧姓。それ以外の場合は、文字列をintと比較していて、決して等しいとは限りません。

answers = [25, 20, 95] 
関連する問題