问题描述
我对python中的while循环有疑问.我想制作一个在特定时间内执行while循环的程序.我想添加一个额外的功能,即在我们运行该程序时,可以通过按一个随机键来更改某个变量.
I have a question about while loops in python.I want to make a program that performs a while loop in a certain time.I want to add the extra feature that while the program us running,a certain variable can be changed by pressing a random key.
from time import sleep
import time
i=0
a=0
while i<10:
i=i+1
i=i+a
a=a+1
time.sleep(1)
print i
我想通过按任意键将变量a重置为0.如果不按任何按钮,循环应保持不变.我应该添加什么命令?
I want to do it that the variable a can be reset to 0 by pressing any key.The loop should continue unchanged if no button is pressed.What command should i add?
谢谢我试过:
import pygame
from pygame.locals import *
import time
i=0
a=0
pygame.init()
while i<10:
pygame.event.get()
i=i+a
print i
keys = pygame.key.get_pressed()
if keys[K_ESCAPE]:
i=0
i=i+1
time.sleep(1)
pygame.quit()
但是现在当我按下按钮时什么也没发生.我想念什么?
But now nothing happens when I press a button.What did i miss?
推荐答案
您可以使用curses.优秀的文档在这里: http://docs.python.org/dev/howto/curses.html #user-input
You can use curses.Excellent doc is here:http://docs.python.org/dev/howto/curses.html#user-input
这篇关于While循环等待输入python的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!