Python 我不知道';我不明白为什么我的函数不';不会发生
我正在尝试写一个打字挑战游戏,玩家必须在时间限制内尽可能快地输入一个单词。在Python 我不知道';我不明白为什么我的函数不';不会发生,python,function,python-3.x,Python,Function,Python 3.x,我正在尝试写一个打字挑战游戏,玩家必须在时间限制内尽可能快地输入一个单词。在game()函数的末尾,当计时器到达0时,应该执行round1()函数。但是,什么也没有发生,它只是停留在数字1上。知道是什么导致了这种行为吗 这是我正在使用的代码: import random import time global timer timer = 20 global counting counting = 10 global rounds rounds = 0 def menu(): print (
game()
函数的末尾,当计时器到达0
时,应该执行round1()
函数。但是,什么也没有发生,它只是停留在数字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()
您正在进入函数
round1
,但一旦进入,就会陷入无限循环,因为变量无用
永远不会更改
即使您在执行
循环时执行,您也将永远无法获胜,因为您在计时器已用完之前不会执行输入。从第一轮开始尝试此代码:
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()
请注意,由于没有定义round2()
函数,因此它将以namererror
失败。一个更好的解决方案是对函数进行一般化,比如round\ux(数字、单词、时间到答案)
;这样你就可以重复使用相同的代码,而不必导入全局代码。我在玩你的游戏,你的开销和错误太多了
所以我只是改变了它的结构来简化:
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()
随机导入
导入时间
当前时间=lambda:int(time.time()*1000)
计时器=20#假设用户有20秒的时间输入一个单词
计数=10
请求的_word=“”
选择=[“时髦”、“比萨饼”、“嘶嘶声”、“豺狼”]
轮数=0
def菜单():
打印(“主菜单\n键入“开始”以开始打字挑战”)
游戏()如果输入()
#并没有必要把比赛分成两局,但我不想搞砸
#你的程序结构太多了
def game():
全局请求字
尝试:
请求的单词=(随机选择)
选项。删除(请求的单词)
除索引器外:
打印“游戏结束”
返回
打印(“您必须键入的单词是”,请求的单词)
打印(“您将根据键入单词所需的时间进行计时。”)
打印(“每轮您将有一个稍微少的时间键入单词”)
打印(“开始输入…”)
对于范围(10,-1,-1)内的计数:
打印(计数)
时间。睡眠(1)
第(轮)
def round():
全局计时器
开始=当前时间()
word=输入(“输入word->”)
停止=当前时间()
时间_delta=停止-启动#以毫秒为单位
如果time_delta
当提示“Enter word->”出现时,用户有所有时间输入一个单词,但在计算完时间增量后,如果超过您的限制,游戏就结束了。注意,这不会像在Windows上那样工作,但在Linux上工作很好。这里不需要并发,这是简单的流程游戏,不需要并发。我没有想过只运行计时器。