随机整数无限循环
该函数在选择一个整数后不会停止,而是无限循环地继续执行该操作。谁能告诉我为什么,或者我该如何修复这个问题?
def wGen():
top = len(Repo.words)
randInt = random.randint(0,len(Repo.words))
print randInt, top
它产生以下输出:(1037 是数据库中的元素数量)
...
214 1037
731 1037
46 1037
490 1037
447 1037
103 1037
342 1037
547 1037
565 1037
90 1037
...
我用这个“类似菜单的函数”调用该函数
def gameMenu():
"""Game Menu"""
gameMenuPrint()
def m():
inp = raw_input('enter option: ')
while inp != 'q':
if inp == 'play' or inp =='1': GameC.wGen()
elif inp == 'help' or inp =='2': pass
elif inp == 'back' or inp =='0': mainMenu()
else:
print 'wrong input!'
inp = raw_input('enter valid a option!: ')
m()
This function dosen't stop after choosing one integer, it keeps doing that in an infinite loop. Can anyone tell me why, or how can I repair this issue?
def wGen():
top = len(Repo.words)
randInt = random.randint(0,len(Repo.words))
print randInt, top
It produces this output: (1037 is the number of elements in the database)
...
214 1037
731 1037
46 1037
490 1037
447 1037
103 1037
342 1037
547 1037
565 1037
90 1037
...
There you go i call the function with this 'menu alike function'
def gameMenu():
"""Game Menu"""
gameMenuPrint()
def m():
inp = raw_input('enter option: ')
while inp != 'q':
if inp == 'play' or inp =='1': GameC.wGen()
elif inp == 'help' or inp =='2': pass
elif inp == 'back' or inp =='0': mainMenu()
else:
print 'wrong input!'
inp = raw_input('enter valid a option!: ')
m()
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这部分是问题所在:
您在进入循环之前要求
raw_input
。一旦进入循环,就不再要求输入。将其更改为:虽然我实际上更喜欢这样:
This part is the problem:
You ask for
raw_input
before entering the loop. Once you enter the loop, you never ask for input again. Change it to this:Though I'd actually prefer this:
while 条件将始终为真,除非他们在第一个 raw_input 中输入“q”。您永远不会获得 inp 的新值。将另一个 raw_input 添加到 while 循环中。
The while condition will always be true unless they enter 'q' in the first raw_input. You never get a new value for inp. Add another raw_input into your while loop.