我正在编写自己的关于大脑中的心理操作的游戏,例如加法,减法,乘法和除法。目前,通过加减运算,游戏正在平稳运行,但是我现在想问的是用户的猜测,但是如果他/她延迟了5秒,则输入消息应该消失,正确的结果应该出现,并进行另一次操作应该出现。这是游戏的以下代码:
def level_one():
goodGuess=0
badGuess=0
time_easy_level = default_timer()
numbers_with_operators=[]
local_operators_easy=["+","-"]
global continuePlaying
#====Repeat operations 5 times=================
for x in range(5):
#===10 random numbers between 1 and 10 are generated =========
easy_level=[randint(1,10) for i in range(1,10)]
#===Each list of random numbers is appended with a random operator===
for item in easy_level:
numbers_with_operators.append(item)
time.sleep(1)
numbers_with_operators.append(local_operators_easy[randint(0,1)])
if len(numbers_with_operators)==18:
numbers_with_operators.append(randint(1,10))
print numbers_with_operators
time_for_guess=time.time()
deadline_for_guess=time_for_guess+5
while time_for_guess<deadline_for_guess:
user_guess=int(raw_input("What is the result? "))
break
computer_result=compute_list(numbers_with_operators)
if user_guess==computer_result:
goodGuess+=1
print "Good guess!"
else:
print "Sorry, that is not the result"
badGuess+=1
print computer_result
del numbers_with_operators[:]
duration=default_timer()-time_easy_level
continuePlaying=False
print "Your results are: \n"
print "Good guesses: "+str(goodGuess)+"\nBad guesses: "+str(badGuess)
print "Total seconds playing:\n"+str(duration)+" seconds"
return continuePlaying
欢迎所有建议,并随时修改我的代码:)
感谢@aenda和@ lennon310提出的建议,并对以下文章进行了一些研究:来自Gary Robinson的非阻塞raw_input代码,原始输入和超时以及python中带有超时的键盘输入,我能够进行一些细微的更改并生成以下代码解决了我的问题。
timeout=7
class AlarmException(Exception):
pass
def alarmHandler(signum,frame):
raise AlarmException
def my_raw_input(prompt,timeout):
signal.signal(signal.SIGALRM,alarmHandler)
signal.alarm(timeout)
try:
userGuess=int(raw_input(prompt))
signal.alarm(0)
return userGuess
except AlarmException:
print "Uh-oh! Time's for that one!"
signal.signal(signal.SIGALRM,signal.SIG_IGN)
return ''
def level_one():
goodGuess=0
badGuess=0
time_easy_level = default_timer()
numbers_with_operators=[]
local_operators_easy=["+","-"]
global continuePlaying
global timeout
#====Repeat operations 5 times=================
for x in range(5):
#===10 random numbers between 1 and 10 are generated =========
easy_level=[randint(1,10) for i in range(1,10)]
#===Each list of random numbers is appended with a random operator===
for item in easy_level:
numbers_with_operators.append(item)
time.sleep(1)
numbers_with_operators.append(local_operators_easy[randint(0,1)])
if len(numbers_with_operators)==18:
numbers_with_operators.append(randint(1,10))
print numbers_with_operators
user_guess = my_raw_input("What's the result? ",timeout)
computer_result=compute_list(numbers_with_operators)
if user_guess==computer_result:
goodGuess+=1
timeout=7
print "Good guess!"
else:
print "Sorry, that is not the result"
badGuess+=1
timeout=7
print computer_result
print "That was the number operation "+str(x)
del numbers_with_operators[:]
duration=default_timer()-time_easy_level
continuePlaying=False
print "Your results are: \n"
print "Good guesses: "+str(goodGuess)+"\nBad guesses: "+str(badGuess)
print "Total seconds playing:\n"+str(duration)+" seconds"
return continuePlaying
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句