简体   繁体   English

列举python中的麻烦

[英]enumerate trouble in python

A little background on the program. 关于程序的一些背景。 it sets up a graphwindow with 2 pictures of the candidates, some instructions and an entry box, the user then puts the abbreviation into the entry box and clicks on one of the candidates and it displays the amount of votes are equivalent to whatever state then entered. 它建立了一个带有两张候选图片,一些说明和一个输入框的图形窗口,然后用户将缩写词放入输入框,然后单击其中一个候选,它显示的票数等于然后输入的任何状态。 I am having trouble writing an exception handler thats supposed to print an error message if the user enters a state that is not in my list. 我在编写异常处理程序时遇到麻烦,如果用户输入的状态不在我的列表中,该异常处理程序应该打印一条错误消息。 heres the section of the code that im having trouble with: 这是我遇到麻烦的代码部分:

    while True:
      vote = win.getMouse()
      voteX = vote.getX()
      voteY = vote.getY()
#Romney
      if voteX >= 163 and voteX <= 237 and voteY <= 100:
         enteredtext = entrybox.getText()
         enteredtextu = enteredtext.upper()
         for i, s in enumerate(STATE_LIST):
            if enteredtextu != s:
                print('Not a state')


            else:
                totalvotesr += STATE_VOTES[enteredtextu]
                votesnumr = ('VOTES:' ,totalvotesr)
                displayvotesr.setText(votesnumr)
                entrybox = Entry(Point(WINDOW_WIDTH/2, WINDOW_HEIGHT/1.1), 10)
                entrybox.draw(win)
                if totalvotesr >= 270:
                    circle_winner(win, Point(WINDOW_WIDTH/4, WINDOW_HEIGHT/12))
                    cross_out_loser(win, Point(WINDOW_WIDTH/(4/3), WINDOW_HEIGHT/12))

STATE_LIST is a list of all 50 states abbreviated. STATE_LIST是所有50个州的缩写列表。 What i tried to do is use enumerate and have it check entereredtextu to all items in the list and if it isnt there it would print "Not a state". 我试图做的是使用枚举,并让它检查entereredtextu到列表中的所有项目,如果不存在,它将打印“ Not a state”。 the problem i am having is that when i enter a state thats not in my list it prints "Not a state" 50 times and when i enter a state that is on the list it displays the amount of votes like its supposed to, but also prints "Not a state" 50 times 我遇到的问题是,当我输入不在列表中的状态时,它会打印“不是状态” 50次;当我进入列表中的状态时,它会显示应有的投票数量,而且打印“不是状态” 50次

You check for every state whether its name matches the entered text, as that check is inside the for loop. 您检查每个状态的名称是否与输入的文本匹配,因为该检查位于for循环内。 So it makes sense that you get 'Not a state' 50 (or 49) times . 因此,您有50(或49)次“不是状态”的感觉是有道理的。

Try the code below instead. 请尝试以下代码。 It checks just once whether enteredtextu is in the list of states, and enters the loop for finding the number of votes only if it is. 它只检查一次enteredtextu是否在状态列表中,并且仅在进入投票数时才进入循环。

...
enteredtext = entrybox.getText()
enteredtextu = enteredtext.upper()
if not enteredtextu in STATE_LIST:
    print 'Not a state'
else:
    for i, s in enumerate(STATE_LIST):
        # ... etc

Here's what's going on. 这是怎么回事。 Let's say the user enters AR : 假设用户输入AR

Let's start 'enumerating' over the list of states: 让我们开始对状态列表进行“枚举”:

AK != AR ? Yup! print 'Not a state'
AL != AR ? Yup! print 'Not a state'
AR != AR ? Oh these are the same! Print the number of votes!
AZ != AR ? Yup! print 'Not a state'
... so on ...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM