2016-09-06 3 views
-2

나는 농업에 대한 기본적인 3 가지 퀴즈를 만들어야합니다. 3 가지 질문을하고, 답이 맞는지 틀린지를 출력하고, 틀린 경우 다시 시도 할 수 있습니다. 또한 점수 기능이 있어야합니다. 내가 질문을 완료하고 사양의 부정확 한/올바른 부분을하지만 내가 뭘하려고해도 나는 점수 기능을 작동시키지 못한다. 나는 시도했다 : 다음퀴즈 득점 기능이 필요합니다.

score = 0 

def counter(score) 
    score = score + 1 

def counter(score) 
    score = 0 
    score = score + 1 

def counter(score) 
    global score 
    score = 0 
    score = score + 1 

와 다음 대답은 정확했다 일단 라인이 읽어 :

counter(score) 

가 나는 또한

score = 0 

다음

score = score + 1 

을 시도 하지만 아무것도 작동하지 않으며 나는 어떤 일이 잘못되고 있는지 알아 봅니다. 또한 사용자가 마지막에 얼마나 많은 사용자를 확보했는지 인쇄해야합니다.

CODE :

score = 0 

def quiz(): 
    print("Here is a quiz to test your knowledge of farming...") 
    print() 
    print() 
    print("Question 1") 
    print("What percentage of the land is used for farming?") 
    print() 
    print("a. 25%") 
    print("b. 50%") 
    print("c. 75%") 
    answer = input("Make your choice: ") 
    if answer == "c": 
     print("Correct!") 
     score = score + 1 
    else: 
     print("Incorrect.") 
     answer = input("Try again! ") 
     if answer == "c": 
      print("Correct") 
      score = score + 1 
     else: 
      print("Incorrect! Sorry the answer was C.") 
    print() 
    print() 
    print("Question 2") 
    print("Roughly how much did farming contribute to the UK economy in 2014.") 
    print() 
    print("a. £8 Billion.") 
    print("b. £10 Billion.") 
    print("c. £12 Billion.") 
    answer = input("Make your choice: ") 
    if answer == "b": 
     print("Correct!") 
     score = score + 1 
    else: 
     print("Incorrect.") 
     answer = input("Try again! ") 
     if answer == "b": 
      print("Ccrrect!") 
      score = score + 1 
     else: 
      print("Incorrect! Sorry the answer was B.") 
    print() 
    print() 
    print("Question 3.") 
    print("This device, which was invented in 1882 has revolutionised farming. What is it called?") 
    print() 
    print("a. Tractor") 
    print("b. Wagon.") 
    print("c. Combine.") 
    answer == input("Make your choice. ") 
    if answer == "a": 
     print("Correct!") 
     score = score + 1 
    else: 
     print("Incorrect.") 
     answer == input("Try again! ") 
      if answer == "a": 
       print("Correct!") 
       score = score + 1 
      else: 
       print("Incorrect! Sorry the answer was A.") 

print("You got {0}/3 right!".format(score)) 
+4

'def'아래의 모든 것이 들여 쓰기되어야합니다. – elethan

+2

힌트 : [함수는 무언가를 반환 할 수 있습니다.] (https://learnpythonthehardway.org/book/ex21.html). – LaundroMat

+0

파이썬 코드를 게시하는 경우 들여 쓰기를 정확하게 재현해야합니다. 심하게 들여 쓰여진 파이썬 코드는 난센스 다. – khelwood

답변

1

n00b (및 작업) 방법으로하는 global 키워드는 외부을 을 선언 한 전역 변수의 사용 (수

score = 0 

def quiz(): 
    global score 

그런 짓을하는 것입니다 귀하의 기능 quiz). 코드를 들여 쓰기하지 않았기 때문에 quiz 함수 안이나 바깥쪽에있는 마지막 문장을 인쇄하고 있는지는 명확하지 않지만 문제가되지는 않습니다. score과 퀴즈 기능 또는 둘 다 외부에 인쇄하면 괜찮을 것입니다. 숙제로 행운을 빌어 요!

0

그래서 파이썬 범위는 들여 쓰기로 정의됩니다 (위 주석에 언급 된대로). 따라서 범위 레이어를 만드는 모든 문장은 들여 쓰기로 구분됩니다. 클래스, 함수, 루프 및 부울 문을 예로들 수 있습니다. 여기에 그 예가 있습니다.

Class A: 
    def __init__(self, msg): 
     self.message = msg 
    def print_msg(self): # prints your message 
     print self.message 
    def is_hi(self): 
     if self.message == "hi": 
      return true 
     else: 
      return false 

이것은 존재할 수있는 범위의 여러 계층을 보여줍니다. 함수를 정의하고 그 범위에 아무 것도 넣지 않았기 때문에 코드가 제대로 작동하지 않습니다. 그 오류가 사라지기 위해서는 함수의 범위 내에 무엇이든 넣어야 할 것입니다.하지만 그것은 아마도 여러분이 찾고있는 것이 아닙니다.

0
import sys 
def quiz(): 
    score = 0 
    print("Here is a quiz to test your knowledge of farming...") 
    print() 
    print() 
    print("Question 1") 
    print("What percentage of the land is used for farming?") 
    print() 
    print("a. 25%") 
    print("b. 50%") 
    print("c. 75%") 
    answer = input("Make your choice: ") 
    if answer == "c": 
     print("Correct!") 
     score = score + 1 
    else: 
     print("Incorrect! Sorry the answer was C.") 
    print() 
    print() 
    print("Question 2") 
    print("Roughly how much did farming contribute to the UK economy in 2014.") 
    print() 
    print("a. £8 Billion.") 
    print("b. £10 Billion.") 
    print("c. £12 Billion.") 
    answer = input("Make your choice: ") 
    if answer == "b": 
     print("Correct!") 
     score = score + 1 
    else: 
     print("Incorrect! Sorry the answer was B.") 
    print() 
    print() 
    print("Question 3.") 
    print("This device, which was invented in 1882 has revolutionised farming. What is it called?") 
    print() 
    print("a. Tractor") 
    print("b. Wagon.") 
    print("c. Combine.") 
    answer = input("Make your choice: ") 
    if answer == "a": 
     print("Correct!") 
     score = score + 1 
    else: 
     print("Incorrect! Sorry the answer was A.") 
    print("You got {}/3 right!".format(score)) 
def main(): 
    quiz(); 
if __name__ == "__main__": 
    main() 

코드와이 코드를 비교하여 내가 만든 변경 사항을 확인하십시오.이 변경 사항이 도움이 되었기를 바랍니다. 내가 전에 몇 가지 이동 :

  1. 가 '='과의 차이를 알고 '=='. '='는 대입 연산자이므로 변수를 일부 값과 동일하게 설정합니다. '=='은 테스트 목적으로 사용됩니다 (예 : if 5 == 3 + 2 : print 'True' if 문에이 문제가 있음을 발견했습니다. 코드 : 당신이 '주()의 경우 이름 == "주요을"'이있는 경우

  2. 끝의 주요 기능은 항상 실행됩니다.

관련 문제