我不明白为什么我的功能不会发生

时间:2016-07-07 17:02:34

标签: python function python-3.x

我正在尝试编写打字挑战游戏,其中玩家必须在时间限制内尽快输入单词。在game()函数结束时,当计时器到达round1()时,它应该执行0函数。但是,没有任何反应,只会保持在1的数字上。是什么原因导致了这种行为?

这是我正在使用的代码:

import random
import time
global timer
timer = 20
global counting
counting = 10
global rounds
rounds = 0
def menu():
    print ("Main Menu\nType in 'start' to begin the typing challenge")
    start = input()
    if start == "start":
        game()
    else:
        menu()
def game():
    global counting
    choices = ["snazzy", "pizzas", "sizzle", "jackal"]   
    global word
    word = (random.choice(choices))
    print ("The word you must type is", word)
    print ("You will be timed on how long it takes you to type the word.")
    print ("Each round you will have a slightly smaller amount of time to type the word")
    time.sleep(10)
    print ("Starting in...")
    for count in range(10):
        print (counting)
        time.sleep(1)
        counting -=1  
    round1()
def round1():
    useless = 100
    global rounds
    global word
    global timer
    while useless > 1:
        for count in range(20):
            time.sleep(1)
            timer -=1
    print ("Type", word)
    attempt = input()
    if attempt == word and timer > 0:
        rounds = rounds+1
        round2()
    else:
        lose()

3 个答案:

答案 0 :(得分:1)

你进入函数round1,但是一旦你在那里,你就会陷入无限循环,因为变量useless永远不会改变。

即使您取出while循环,也永远无法获胜,因为直到计时器用完之后才接受输入。

答案 1 :(得分:0)

从第1轮开始尝试此代码:

def round1():
    import sys, select
    global rounds
    global word
    global timer
    print ("Type", word)
    input, out, error = select.select( [sys.stdin], [], [], timer)
    if (input):
        attempt = sys.stdin.readline().strip()
        if attempt == word:
            rounds = rounds+1
            round2()
        else:
            lose()
    else:
        lose()

if __name__ == '__main__':
    menu()

请注意,由于您没有定义NameError函数,因此它将失败并显示round2()。更好的解决方案是使用类似round_x(number, word, time_to_answer)之类的功能来推广您的功能;这样,您可以重用相同的代码,而不必导入全局变量。

答案 2 :(得分:0)

我正在玩你的游戏,你做了太多的开销和错误 所以我只是改变了它的结构以简化:

import random
import time
current_milli_time = lambda:int(time.time() * 1000)
timer = 20 #suppose user has 20 secs to enter a word
counting = 10
requested_word = ""
choices = ["snazzy", "pizzas", "sizzle", "jackal"]   
rounds = 0
def menu():
    print ("Main Menu\nType in 'start' to begin the typing challenge")
    game() if input().lower() == "start" else menu()
#There is no need to split game and round but I didn't want to mess with
#your program structure too much
def game():
    global requested_word
    try:
        requested_word = (random.choice(choices))
        choices.remove(requested_word)
    except IndexError:
        print "Game is finished"
        return
    print ("The word you must type is", requested_word)
    print ("You will be timed on how long it takes you to type the word.")
    print ("Each round you will have a slightly smaller amount of time to type the word")
    print ("Starting typing in in...")
    for count in range(10,-1,-1):
        print (count)
        time.sleep(1)
    round()
def round():
    global timer
    start = current_milli_time()
    word = input("Enter word -> ")
    stop = current_milli_time()
    time_delta = stop-start #this is in milliseconds
    if time_delta< timer*1000 and word == requested_word :
        timer = timer-5 #lower the time to enter a word
        game()
    else:
        print("Game OVER")
menu()

当提示&#39;输入字词 - &gt;&#39;看起来用户总是想要输入一个单词,但是在计算出time_delta之后,如果它超过了你的限制,那么它的游戏结束了。