【问题标题】:yes/no input loop, issues with with correct looping是/否输入循环,正确循环的问题
【发布时间】:2016-10-12 21:15:32
【问题描述】:

我正在编写一个简短的猜谜游戏。但是,我不知道如何防止错误输入(是/否),从而阻止用户继续前进。这是一个工作代码。尝试使用 while True 但它只会使输入更加混乱。最远的是 else 通知玩家,但 q 计数向前移动。

# -*- coding: cp1250 -*-

import sys

def genVprasanja ():

    yes = set(['yes','y', ''])
    no  = set(['no','n'])
    testQ = ['hello1', 'hello33112', 'hello332', 'hello2', 'hello4','hellomore', 'ANSWER', 'hello1', 'hello33112', 'hello332', 'hello2', 'hello4','hellomore', 'ANSWER2', 'hello1', 'hello33112', 'hello332', 'hello2', 'hello4','hellomore', 'ANSWER3'] 
    points = 5
    total = 0
    for x in range(0,3):
        for y in xrange(len(testQ)):
            reply = str(raw_input('\n'+str(abs(y-5))+ ' - ' +  testQ[y]+' (y/n) --> ')).lower().strip()
            if reply in yes:
                print '\ncorect!\n\nAnswer is :',testQ[5], '\n\nPoints: ',points, '\n'
                total = total + points
                print 'Total: ', total, '\n'
                break                    
            elif reply in no:
                points = points - 1      
                if points !=  0:     
                    print '\nwrong!', '\n\nNext question for: ',points                                     
                else:
                    print '\nThe end!\n\n' 'Every anwser is wrong!\n\nYou got 0 points.\n\Correct answer is:', testQ[5],'\n\n'
                    total = total + points
                    print 'SKUPNE TOČKE: ', total
                    break
            else:
                sys.stdout.write("\nPlease press 'RETURN' or 'N'\n")
    points = 5
genVprasanja()

编辑:

每位玩家都可以回答三组 5 个问题。他们收到问题,直到他们说是。如果他们说没有 5 次循环结束(3 次) - 我正在使用 var points 来计数。

但是,如果他们输入了错误的单词(不是 no 和 not yes),输入的问题就会重复自己再次询问他们(直到他们输入一个有效的答案)。之后,他们得到了同样的问题,他们未能正确回答。

【问题讨论】:

  • 太多的空白...
  • 函数体需要缩进。
  • 真的很抱歉,我在分解代码,发帖的时候忘记去掉ws了....
  • raw_input() 返回一个字符串,不需要使用str() 将其转换为字符串。
  • if (reply not in yes) and (reply not in no): continue,其中continue 重复循环

标签: python loops


【解决方案1】:

while True: 可以工作,一旦满足条件,您需要将break 退出循环。

while True:
    reply = str(raw_input('\n' + str(abs(y - 5)) + ' - ' + testQ[y] + ' (y/n) --> ')).lower().strip()
    if reply in yes or reply in no:
            break

根据更新的范围,试试这个,似乎break 可能会导致您出现问题:

reply = False
while reply not in yes and reply not in no:
    reply = str(raw_input('\n' + str(abs(y - 5)) + ' - ' + testQ[y] + ' (y/n) --> ')).lower().strip()

【讨论】:

  • 这有点用,但现在如果我按是,它会从一个新问题开始,而不是从列表中的第一个问题开始。 “不”也是如此。它一直到最后一个,并从最后一个开始,用于另一组。
  • 也许我不明白您想要达到的目标,请您详细解释一下您希望程序如何运行。
  • 如果我说是会发生什么,如果我说不是会发生什么?
  • 程序最多询问用户 5 个问题(第 6 个问题是答案)。如果用户对其中任何一个说“是”,则循环将在该点停止。在那之后,他得到了更多的5个问题。同样的规则适用。但是如果他说不,他会从集合中得到所有问题(直到他达到五个问题,如果用户说是,循环中断,然后他又得到两组问题(想象这个列表有 12 个、18 个或更多)条目)。但如果他说不
  • 您能否更新您的问题以反映这一点,这有点棘手。
【解决方案2】:

你需要一个'while'在这里的某个地方,一旦你打到'for'块的末尾,它将转到范围中的下一个值。

for y in xrange(len(testQ)):
    bGoodInput = False
    while not bGoodInput:
       reply = str(raw_input('\n'+str(abs(y-5))+ ' - ' +  testQ[y]+' (y/n) --> ')).lower().strip()
       if reply in yes:
           ...
           bGoodInput = True

       elif reply in no:
           ...
           bGoodInput = True

       else:
           ...

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多