跟踪得分

时间:2015-04-21 21:14:07

标签: python python-3.x

我正在尝试进行测验,向用户显示州名,他们必须正确说明资本。一切正常,除了跟踪用户的分数。我试图改变代码的得分部分,但没有任何工作!我认为问题出现在nextCapital()函数中的某个地方,但是我又错了。我是python的新手,所有这一切都有点压倒性的。我真的很感激你的帮助!

import tkinter
import random


capitals={"Washington":"Olympia","Oregon":"Salem",\
                "California":"Sacramento","Ohio":"Columbus",\
                "Nebraska":"Lincoln","Colorado":"Denver",\
                "Michigan":"Lansing","Massachusetts":"Boston",\
                "Florida":"Tallahassee","Texas":"Austin",\
                "Oklahoma":"Oklahoma City","Hawaii":"Honolulu",\
                "Alaska":"Juneau","Utah":"Salt Lake City",\
                "New Mexico":"Santa Fe","North Dakota":"Bismarck",\
                "South Dakota":"Pierre","West Virginia":"Charleston",\
                "Virginia":"Richmond","New Jersey":"Trenton",\
                "Minnesota":"Saint Paul","Illinois":"Springfield",\
                "Indiana":"Indianapolis","Kentucky":"Frankfort",\
                "Tennessee":"Nashville","Georgia":"Atlanta",\
                "Alabama":"Montgomery","Mississippi":"Jackson",\
                "North Carolina":"Raleigh","South Carolina":"Columbia",\
                "Maine":"Augusta","Vermont":"Montpelier",\
                "New Hampshire":"Concord","Connecticut":"Hartford",\
                "Rhode Island":"Providence","Wyoming":"Cheyenne",\
                "Montana":"Helena","Kansas":"Topeka",\
                "Iowa":"Des Moines","Pennsylvania":"Harrisburg",\
                "Maryland":"Annapolis","Missouri":"Jefferson City",\
                "Arizona":"Phoenix","Nevada":"Carson City",\
                "New York":"Albany","Wisconsin":"Madison",\
                "Delaware":"Dover","Idaho":"Boise",\
                "Arkansas":"Little Rock","Louisiana":"Baton Rouge"}


score=0
timeleft=30

print("This program will launch a capital quiz game.")
input1 = input("What difficulty would you like to play: easy, normal, or hard?\n")
if input1.lower() == "easy":
    seconds = 90
    timeleft = seconds
elif input1.lower() == "normal":
    seconds = 60
    timeleft = seconds
elif input1.lower() == "hard":
    seconds = 30
    timeleft = seconds

def startGame(event):

    #if there's still time left...
    if timeleft == seconds:
        #start the countdown timer.
        countdown()
    #run the function to choose the next colour.
    nextCapital()
    if timeleft == 0:

        endlabel = tkinter.Label(root, text="The time is up!\nYour score is: " + str(score) +" out of 50", font=('Helvetica', 12))
        endlabel.pack()
        e.pack_forget()
#function to choose and display the next colour.
def nextCapital():

    #use the globally declared 'score' and 'play' variables above.
    global score
    global timeleft

    #if a game is currently in play...
    if timeleft > 0:

        #...make the text entry box active.
        e.focus_set()


        randchoice = random.choice(list(capitals.keys()))
        answer = capitals.get(randchoice)
        if answer.lower() == randchoice.lower():
            score = score+1

####        #this deletes the random choice from the dictionary    
####        del capitals[randchoice]

        #clear the text entry box.
        e.delete(0, tkinter.END)
        #this updates the random choice label 
        label.config(text=str(randchoice))
    #update the score.
    scoreLabel.config(text="Score: " + str(score))

#a countdown timer function. 
def countdown():

    #use the globally declared 'play' variable above.
    global timeleft

    #if a game is in play...
    if timeleft > 0:

        #decrement the timer.
        timeleft -= 1
        #update the time left label.
        timeLabel.config(text="Time left: " + str(timeleft))
        #run the function again after 1 second.
        timeLabel.after(1000, countdown)

#create a GUI window.
root = tkinter.Tk()
#set the title.
root.title("Capital Quiz")
#set the size.
root.geometry("500x250")

#add an instructions label.
instructions = tkinter.Label(root, text="Brush up your geography skills!", font=('Helvetica', 12))
instructions.pack()

#add a score label.
scoreLabel = tkinter.Label(root, text="Press enter to start" + str(score), font=('Helvetica', 12))
scoreLabel.pack()

#add a time left label.
timeLabel = tkinter.Label(root, text="Time left: " + str(timeleft), font=('Helvetica', 12))
timeLabel.pack()

#prompt label
promptLabel = tkinter.Label(root, text= "Enter the capital of: ", font=('Helvetica', 12))
promptLabel.pack()

#add a label that will hold print the prompt
label = tkinter.Label(root, font=('Helvetica', 60))
label.pack()

#add a text entry box for typing in colours.
e = tkinter.Entry(root)
#run the 'startGame' function when the enter key is pressed.
root.bind('<Return>', startGame)
e.pack()
#set focus on the entry box.
e.focus_set()

#start the GUI
root.mainloop()

2 个答案:

答案 0 :(得分:3)

randchoicecapitals dict中的之一,即状态。

answercapitals字典中的之一,即资本

然后,您比较randchoiceanswer的小写版本,如果它们相等,则增加分数。但显然他们永远不会平等(一个是国家,一个是资本)。因此,您的分数将无法正确更新。

答案 1 :(得分:0)

不是答案,而是一些建议:避开所有全局变量,用明确定义的简短方法将事物包装到对象中。您将有更多 更轻松地处理和推理代码。

考虑一个类的骨架:

class Quiz(object):

  def __init__(self, difficulty, capitals_dict):
    self.score = 0
    self.capitals = capitals
    self.difficulty = ...
    self.right_answer = None

  def getNextQuestion(self):
    # Choose a capital and assign it as the current right answer
    ...
    self.right_answer = ...
    return self.right_answer # to show it to user

  def checkAnswer(user_answer):
    if user_answer == self.right_answer:
      self.score = ...
      return True
    else:
      ...

  def isGameOver(self):
    return len(self.capitals) == 0

我想它很清楚如何使用这样的类,并合理地清楚如何实现它。