2012-03-05 57 views
0

我正在學習Python並正在製作Q &腳本。我爲這些問題提出了一個函數。這很順利。現在我想要一個平均功能。如果可能的話,我想避免使用全局變量。我知道我的變量重置在頂部......有人可以給我一些指針嗎?我知道C/PHP/BASIC,並且想要掌握這個語言。以下是我的問題功能。函數問題和局部變量

def q(question, a, b, c, c_answer): 
     total,tally=0,0 
     print "",question 
     print " a.",str(a) 
     print " b.",str(b) 
     print " c.",str(c) 
     u_answer = raw_input() 
     if c_answer == "a" and u_answer == "a": 
      print "Correct, the answer is A!" 
      tally+=1 
     elif c_answer == "b" and u_answer == "b": 
      print "Correct, the answer is B!" 
      tally+=1 
     elif c_answer == "c" and u_answer == "c": 
      print "Correct, the answer is C!" 
      tally+=1 
     else: 
      print "I am sorry, but the correct answer is",c_answer 
     print "\..n" 
     total+=1 
+0

哪個版本ru使用? – Teja 2012-03-05 19:11:32

+0

我正在使用版本2.7 – 2012-03-05 19:13:28

+0

定義「平均功能」。 – delnan 2012-03-05 19:13:43

回答

0

既然你剛開始學習Python的,我重新格式化你的代碼更Python。

def q(question, a, b, c, c_answer): 
    total, tally = 0, 0 
    print "", question 
    print " a. %s" % a 
    print " b. %s" % b 
    print " c. %s" % c 
    u_answer = raw_input("your answer? ").strip() 
    if c_answer == u_answer: 
     print "Correct, the answer is %s!" % u_answer 
     tally += 1 
    else: 
     print "I am sorry, but the correct answer is %s" % c_answer 
    print "\n" 
    total += 1 

要真正回答你的問題:

有幾種方法可以跟蹤的問題和正確答案總數不使用全局級變量(實際上是模塊級,但是這一個不同的話題;)。

一個是在當前總計通過,已根據當前問題q重新計算,然後將它們傳回了:

def q(question, a, b, c, c_answer, total, tally): 
    print "", question 
    print " a. %s" % a 
    print " b. %s" % b 
    print " c. %s" % c 
    u_answer = raw_input("your answer? ").strip() 
    if c_answer == u_answer: 
     print "Correct, the answer is %s!" % u_answer 
     tally += 1 
    else: 
     print "I am sorry, but the correct answer is %s" % c_answer 
    print "\n" 
    total += 1 
    return total, tally 

然後在你的主要程序,你可以說:

question_pool = (
    ('What is 2 + 2?', 2, 3, 4, 'c'), 
    ('What is blue mixed with yellow?', 'green', 'orange', 'pink', 'a'), 
    ('How far does light travel in one nanosecond?', '10 mm', '20 cm', '30 m', 'b'), 
    ) 

total, tally = 0, 0 
for packet in question_pool: 
    question, a, b, c, answer = packet 
    total, tally = q(question, a, b, c, answer, total, tally) 

print "you answered %d correctly, for a score of %2.0f%%" % (tally, 100.0 * tally/total) 

但是,q只處理問題會更好,不用擔心跟蹤回答了多少個問題紅色以及問了多少問題。的

所以不是接受totaltally,重新計算,並返回totaltallyq現在只返回0如果答案是錯的,1如果這是正確的:

def q(question, a, b, c, c_answer): 
    print "", question 
    print " a. %s" % a 
    print " b. %s" % b 
    print " c. %s" % c 
    u_answer = raw_input("your answer? ").strip() 
    if c_answer == u_answer: 
     print "Correct, the answer is %s!\n" % u_answer 
     return 1 
    print "I am sorry, but the correct answer is %s" % c_answer 
    return 0 

和的休息代碼看起來像:

question_pool = (
    ('What is 2 + 2?', 2, 3, 4, 'c'), 
    ('What is blue mixed with yellow?', 'green', 'orange', 'pink', 'a'), 
    ('How far does light travel in one nanosecond?', '10 mm', '20 cm', '30 m', 'b'), 
    ) 

total, tally = 0, 0 
for packet in question_pool: 
    question, a, b, c, answer = packet 
    tally += q(question, a, b, c, answer) 
    total += 1 

print "you answered %d correctly, for a score of %.0f%%" % (tally, 100.0 * tally/total) 
+0

Ooh謝謝soooo much – 2012-03-05 20:18:27

+0

在C++或BASIC中做這件事並沒有麻煩我...... Python就是這樣猜測! – 2012-03-05 20:19:29

4

q功能刪除total。相反,返回1,如果問題是答對,否則爲0:

def q(question, a, b, c, c_answer): 
    ... 
    return tally 

num_correct = 0 
for question in questions: 
    num_correct += q(...) 

average = float(num_correct)/len(questions) 

如果你不想使用全局變量,只是在組織功能或類方法代碼:

def ask_questions(questions): 
    num_correct = 0 
    for question in questions: 
     num_correct += q(...) 
    return num_correct 

def report_average(num_correct, num_questions): 
    average = float(num_correct)/num_questions 
    print(average) 

num_correct = ask_questions(questions) 
report_average(num_correct, len(questions)) 

我想基本思想是使用return將需要的值傳遞給下一個函數。如果有很多數據要跟蹤,您可以改爲使用類方法。通過使用類,可以作爲實例屬性,而不是使用return存儲值:

class Exam(object): 
    def __init__(self, questions): 
     self.num_correct = 0 
     self.questions = ... 

    def q(self, question, a, b, c, c_answer): 
     ... 
     if correct: 
      self.num_correct += 1 

    def ask_questions(self): 
     for question in self.questions: 
      self.q(question) 

    def report_average(self): 
     average = float(self.num_correct)/len(self.questions) 
     print(average) 
+0

我有點懂得 – 2012-03-05 19:19:43

+0

是的,我想我可以做這個工作。謝謝! – 2012-03-05 19:21:35

+0

我知道「...」意思是插入我的代碼,但我很困惑我在哪裏打印我的問題,並從A,B或C池中檢查我的答案:-( – 2012-03-05 20:12:18