2013-10-23 60 views
0

我與蟒蛇進行實驗,我做了這個小的數學遊戲。儘管我在評分系統方面存在一些問題。每當玩家得到的答案是正確的,我想得分1.我試圖遞增,但我還沒有得到它每一個球員得到一些正確的時間增加。 赫雷什代碼Python的數量遞增

import operator 
import random 

operations = { 
    "addition": ("+", operator.add), 
    "substraction": ("-", operator.sub), 
    "multiplication": ("*", operator.mul), 
    "division": ("/", operator.floordiv), 
} 

def ask_operation(difficulty, maxtries=3): 
    maxvalue = 5 * difficulty 
    x = random.randint(1, maxvalue) 
    y = random.randint(1, maxvalue) 
    op_name, (op_symbol, op_fun) = random.choice(list(operations.items())) 
    result = op_fun(x, y) 
    score = 0 

    print("Difficulty level %d" % difficulty) 
    print("Now lets do a %s calculation and see how clever you are." % op_name) 
    print("So what is %d %s %d?" % (x, op_symbol, y)) 

    for ntry in range(1, 1+maxtries): 
     answer = int(input(">")) 
     if answer == result: 
      print("Correct!") 
      score += 1 
      print score 
      return True 

     elif ntry == maxtries: 
      print("That's %s incorrect answers. The end." % maxtries) 
     else: 
      print("That's not right. Try again.") 
    return False 

def play(difficulty): 
    while ask_operation(difficulty): 
     difficulty += 1 
    print("Difficulty level achieved: %d" % difficulty) 

play(1) 
+4

在整個帖子中,您的縮進是否正確? –

+0

沒有它沒有。我複製並從文本編輯粘貼,所以我失去了格式化。我會編輯 – user2379090

+3

耶修復縮進請。目前,看起來你將每個新問題的分數設置爲0:/ – Dunno

回答

2

比分是復位爲0 ask_operation每一次。您應該用play來初始化它。

順便說一句,//Increment score//不是有效的Python。您可以像這樣在Python中設置註釋,即使在堆棧溢出中也是如此。

score += 1 # Increment score 
+0

啊,是的,對不起,我有時會混淆我的語法。謝謝,我將如何初始化它在播放功能?對不起,我對Python非常陌生。 – user2379090

+0

以同樣的方式你'ask_operation'一樣。確保你在while循環之前保留它,所以它只運行一次。 – Wiwiweb

+0

你解決了你的問題嗎?如果是這樣,請接受答案:) – Wiwiweb