2016-11-08 12 views
-2

これは私がif、whileと関数を使った最初のPythonプログラムです。私もパラメータを渡しました。問題はIFです。手伝って頂けますか?ユーザーに2つの試行をして終了させたいと思っていました。正しい場合は終了しますが、正しくない場合は停止しません。ループが維持されます。IFが必要ですか? Pythonで

は「」

q1Answer="c" 

def questionOne(): 
    print("Here is a quiz to test your knowledge of computer science...") 
    print() 
    print("Question 1") 
    print("What type of algorithm is insertion?") 
    print() 
    print("a....searching algorithm") 
    print("b....decomposition ") 
    print("c....sorting algorithm ") 
    print() 


def checkAnswer1(q1Answer): #q1Answer is a global variable and is needed for this function so it goes here as a parameter 
    attempt=0  #These are local variables 
    score=0 
    answer = input("Make your choice >>>> ") 
    while attempt <1: 
     if answer==q1Answer: 
      attempt= attempt+1 
      print("Correct!") 
      score =score + 2 
      break 

     elif answer != q1Answer: 
      answer =input("Incorrect response – 1 attempt remaining, please try again: ") 
      if answer ==q1Answer: 
       attempt = attempt + 1 
       print("Correct! On the second attempt") 
       score =score + 1 
       break 
     else: 
      print("That is not correct\nThe answer is "+q1Answer) 
      score =0 

    return score # This is returned so that it can be used in other parts of the program 


##def questionTwo(): 
## print("Question 2\nWhat is abstraction\n\na....looking for problems\nb....removing irrelevant data\nc....solving the problem\n") 

def main(): 
    q1answer = questionOne() 

    score = checkAnswer1(q1Answer) 
    print ("Your final score is ", score) 

main() 
+0

質問のこの種のコードレビューに適し次のようになります。http://codereview.stackexchange.com/ – jhaagsma

+1

@jhaagsmaない本当に意図したとおり、それは働いていないからです。 – MooingRawr

+0

フェア十分です。 – jhaagsma

答えて

0

問題は、彼らは二度目、それが間違って取得する場合、あなたが試みをインクリメントしていないです「」「これは、コンピュータ科学のクイズです」。あなただからあなたelifブロックは次のようになりbreak

後に別のattempt = attempt + 1(あるいはattempt += 1)が必要です:

elif answer != q1Answer: 
     answer =input("Incorrect response – 1 attempt remaining, please try again: ") 
     if answer ==q1Answer: 
      attempt = attempt + 1 
      print("Correct! On the second attempt") 
      score =score + 1 
      break 
     attempt = attempt + 1 

これは失敗をtiggering、彼らは二度目に失敗してもインクリメントする試みカウンターを可能とすることループの終わり。

0

ループの後に+ = 1を追加するだけです。

q1Answer="c" 
def questionOne(): 
    print("Here is a quiz to test your knowledge of computer science...") 
    print() 
    print("Question 1") 
    print("What type of algorithm is insertion?") 
    print() 
    print("a....searching algorithm") 
    print("b....decomposition ") 
    print("c....sorting algorithm ") 
    print() 


def checkAnswer1(q1Answer): #q1Answer is a global variable and is needed for this function so it goes here as a parameter 
    attempt=0  #These are local variables 
    score=0 
    answer = input("Make your choice >>>> ") 
    while attempt <1: 
     if answer==q1Answer: 
      attempt= attempt+1 
      print("Correct!") 
      score =score + 2 
      break 

     elif answer != q1Answer: 
      answer =input("Incorrect response – 1 attempt remaining, please try again: ") 
      if answer ==q1Answer: 
       attempt = attempt + 1 
       print("Correct! On the second attempt") 
       score =score + 1 
       break 
     else: 
      print("That is not correct\nThe answer is "+q1Answer) 
      score =0 
     attempt += 1 
     break 
    return score # This is returned so that it can be used in other parts of the program 


##def questionTwo(): 
## print("Question 2\nWhat is abstraction\n\na....looking for problems\nb....removing irrelevant data\nc....solving the problem\n") 

def main(): 
    q1answer = questionOne() 

    score = checkAnswer1(q1Answer) 
    print ("Your final score is ", score) 

main() 
関連する問題