随机整数无限循环

发布于 2025-01-02 03:33:51 字数 779 浏览 0 评论 0原文

该函数在选择一个整数后不会停止,而是无限循环地继续执行该操作。谁能告诉我为什么,或者我该如何修复这个问题?

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

三月梨花 2025-01-09 03:33:51

这部分是问题所在:

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!: ')

您在进入循环之前要求 raw_input 。一旦进入循环,就不再要求输入。将其更改为:

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 option: ')

虽然我实际上更喜欢这样:

def m():
    while True:
        inp = raw_input('enter option: ')
        if inp == 'q': break
        elif 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!'

This part is the problem:

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!: ')

You ask for raw_input before entering the loop. Once you enter the loop, you never ask for input again. Change it to this:

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 option: ')

Though I'd actually prefer this:

def m():
    while True:
        inp = raw_input('enter option: ')
        if inp == 'q': break
        elif 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!'
っ〆星空下的拥抱 2025-01-09 03:33:51

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文