2017-11-17 2 views
-6

私は複数の選択肢のクイズでスコアを加算する必要があります。複数の選択肢のスコアを追加

これは私のコードです:

from tkinter import * 
import time, os, random 

global questions 

questions = [] 

questions = [''' 
What does ? equal to: 

43+(5x(6)**8)/8-8+8 = ? 

1) 10356 
2) 1049803 
3) 202367 
4) 12742130 
5) 343321 

''', 
''' 
Find the range, the median, the mode and the mean: 

>>> 232, 4343, 434, 232.4, 43, 5453, 434, 232, 645, 23.3, 53453.3, 434 <<< 

1) 53410.3, 2943.5, 434, 5496.58 
2) 53410.3, 5956, 434, 5453.28 
3) 232, 43, 3452, 421, 642 
4) 232, 43, 43, 434 
5) 232, 5453, 645, 53453.3 

''', 
''' 
In 2004, the minimum wage in Ontario was $7.15. 
In 2008, it was $8.15. What is the rate of change? 

1) $0.90 
2) $3.98 
3) $1.54 
4) $0.40 
5) $1.80 

''', 
''' 
Find the value of n: 

-------------------------------------- 

    43n/2n x (99.99/9)x7 = 99.99n 

-------------------------------------- 

1) n = 99.92 
2) n = 12.43 
3) n = 16.73 
4) n = 104.4 
5) n = 3.98 

''', 
''' 
Which one is NOT a linear equation? 

1) y = 54x*3 
2) y = 23x*7 
3) y = -x 
4) All of the Above 
5) None of the Above 

''', 
''' 
What is the formula for finding the surface area for a sphere? 

1) 4/3πr**3 
2) πr**2 
3) 2πrh + 2πr**2 
4) πd 
5) 4πr**2 

''', 
''' 
Which variable represents the slope? 
y = mx + c 

1) y 
2) m 
3) x 
4) c 
5) None of the above 
''', 
''' 
An isosceles triangle has the top angle of 6x 
and a supplementary angle of a base angle is 21x. What are the 
measures of the angles of isosceles triangle? 

1) 22.5, 78.75, 78.75 
2) 108.97, 35.52, 35.52 
3) 76.8, 51.6, 51.6 
4) 20, 20, 140 
5) 43.6, 5.5, 98.53 

''', 
''' 
A sphere fits inside a rectangular box of 6cm by 6cm by 7cm. 
What is the greatest possible volume of the sphere? 

1) 113.04 cm*2 
2) 110.46 cm*3 
3) 113.04 cm*3 
4) 110.46 cm*2 
5) 142.9 cm*3 

''', 
''' 
If the area of an isosceles triangle is 196 cm**2 
and the height is 19.6 cm, what would it's perimeter be? 

1) 53.9 cm 
2) 64 cm 
3) 76 cm 
4) 32.43 cm 
5) 32.32 cm 

''' 
] 
global answers 
global picked 
global userans 
global ansRandOrder 
global score 

answers = ['2', '1', '4', '3', '5', '5', '2', '1', '3', '2'] 
picked = [] 
userans = [] 
ansRandOrder = [] 
score = 0 

def nexT(): 
    global userans 
    userans.append(e1.get()) 
    print(userans) 
    print(ansRandOrder) 
    global score 
    global i 
    i = 0 
    if userans[i] == ansRandOrder[i]: 
     score += 1 
    i += 1 
    print(score) 
    self.destroy() 
    ques() 

def ques(): 
    global self 
    global e1 
    global ansRandOrder 
    global picked 

    self = Tk() 

    w = 500 
    h = 375 

    ws = self.winfo_screenwidth() 
    hs = self.winfo_screenheight() 

    x = (ws/2) - (w/2) 
    y = (hs/2) - (h/2) 

    self.geometry('%dx%d+%d+%d' % (w, h, x, y)) 

    x = random.randint(0, 9) 
    while x in picked: 
     x = random.randint(0, 9) 

    picked.append(x) 

    ansRandOrder.append(answers[x]) 

    Label(self, text = '\n').pack() 

    Label(self, text = questions[x]).pack() 

    e1 = Entry(self) 

    e1.pack() 

    Label(self, text = '\n').pack() 

    nex = Button(self, text = 'Next Question', command = nexT).pack() 

    self.mainloop() 



ques() 

結果は常にscore = 0です。

スコアを正しく追加するにはどうすればよいですか?

答えて

0

私はあなたのコードを少し実験しましたが、その問題は他の場所にあるようです。なんらかの理由で、スクリプトは常にnexTのifの間違った答えを比較します。それ以外の場合は、古い変数を消去せずにスクリプトを再起動した後、常に正しい答えと考える。

コードを少しきれいにすると、この問題は解消すると思います。あなたのコードをクラスとして書き直すことをお勧めします。これはTkinterスクリプトの一般的なアプローチです。あなたのための主な利点は次のようになります。

  • それは多くの、より読みやすいです代わりにあなただけのクラス(自己の一部として変数を参照、
  • これ以上のグローバル変数(これは、デバッグが容易になるだろう) .variable)
  • プログラムが終了すると(現時点では正しく機能していないように見える)、del MyClassのようなもので終了して、変数クラッタを取り除くことができます。その混乱はおそらくこのバグの動作が時々変化する理由です。

は、Tkinterのクラスベースのアプリケーション構築についていくつかの良い例と説明については、このスレッドを見ている:あなたのクイズのための一例として Best way to structure a tkinter application

を、それは次のようになります。(非機能性)

class Quiz(): 

    def __init__(self): 

     self.questions = [] 
     self.WriteQuestions() 

     self.answers = ['2', '1', '4', '3', '5', '5', '2', '1', '3', '2'] 
     self.picked = [] 
     self.userans = [] 
     self.ansRandOrder = [] 
     self.score = 0 
     #"self." instead of "global" 
     # Any def inside this class can refer to self."variable" and use it or update it. 

     self.Question() 

    def WriteQuestions(self): 
     # All your questions here, just so they're out of the way. 


    def NextQuestion(self): 

     self.userans.append(e1.get()) 
     self.i = 0 
     if self.userans[i] == ansRandOrder[i]: 
      self.score += 1 
     i += 1 
     print(self.score) 
     root.destroy() # You'll need to pick a name other than "self" for your tk window 
     self.Question() 
     # self is a reserved keyword for classes and shouldn't be used for anything other 
     # than telling a class that you're referring to one of its own variables or functions. 


    def Question(self): 

     # etc... 



if __name__ == 'main': 

    # set up tkloop here 
    # create an instance of the Quiz class, e.g. 
    Qz = Quiz() 

    # finally, after the loop finishes: 

    del Qz # To get rid of the old variables 
関連する問題