如何在Python中合并if / elif语句

时间:2012-03-15 14:36:13

标签: if-statement arguments

我环顾四周并做了一些研究,但由于某种原因,我仍然无法按计划工作。基本上,作为一个初学者,我写了一个Mastermind代码游戏 - 相同的规则和所有。这是代码:

import random
trial = 0
def geuss():
    geuss = raw_input("What is your geuss? ")
    global g1
    global g2
    global g3
    global g4
    a = geuss[:-3]
    b = geuss[1:-2]
    c = geuss[2:-1]
    d = geuss[3:]
    if a == peg1:
        g1 = 'R'
    elif a == peg2:
        g1 = 'W'
    elif a == peg3:
        g1 = 'W'
    elif a == peg4:
        g1 = 'W'
    else:
        g1 = 'X'

    if b == peg2:
        g2 = 'R'
    elif b == peg1:
        g2 = 'W'
    elif b == peg3:
        g2 = 'W'
    elif b == peg4:
        g2 = 'W'
    else:
        g2 = 'X'

    if c == peg3:
        g3 = 'R'
    elif c == peg1:
        g3 = 'W'
    elif c == peg2:
        g3 = 'W'
    elif c == peg4:
        g3 = 'W'
    else:
        g3 = 'X'

    if d == peg4:
        g4 = 'R'
    elif d == peg1:
        g4 = 'W'
    elif d == peg2:
        g4 = 'W'
    elif d == peg3:
        g4 = 'W'
    else:
        g4 = 'X'

    print g1, g2, g3, g4
    global trial
    trial = trial + 1
    return trial



colour = ['B', 'G', 'Y', 'P', 'R']

peg1 = random.choice(colour)
peg2 = random.choice(colour)
peg3 = random.choice(colour)
peg4 = random.choice(colour)
g1 = 0
g2 = 0
g3 = 0
g4 = 0

print ""
while g1 != 'R' or g2 != 'R' or g3 != 'R' or g4 != 'R':
    geuss()
print "Congratulations! It took you %d tries to crack the code!" % trial

print ""
print "The code was %s%s%s%s." % (peg1, peg2, peg3, peg4)

正如您所看到的,函数'geuss()'中的if和elif语句是不必要的错误 - 但是当我尝试将它们组合在一起时,脚本总是会放置W。

    if a == peg1:
        g1 = 'R'
    elif a == peg2 or peg 3 or peg4:
        g1 = 'W'
    else:
        g1 = 'X'

即使我把“QWER”作为输入,我也会得到一个X.有没有什么方法可以巩固它们,同时仍能得到正确的答案?

此外,关于主题,如果有任何其他建议,你可以给我在脚本,因为我是一个初学者,非常感谢!谢谢!

3 个答案:

答案 0 :(得分:2)

使用orand时,您需要指定正在使用的操作。这是你写的......

if a == peg1:
  g1 = 'R'
elif a == peg2 or peg 3 or peg4:
  g1 = 'W'
else:
  g1 = 'X'

elif询问a是否等于peg2,然后是peg3还是peg4。您需要更改它a == peg3/4。像这样......

elif a == peg2 or a == peg3 or a == peg4:

答案 1 :(得分:1)

关于你的if / elif问题,一个典型的Python习惯用法,用于为switch语句使用字典。

// http://stackoverflow.com/questions/374239/why-doesnt-python-have-a-switch-statement
{'option1': function1,
 'option2': function2,
 'option3': function3,
 'option4': function4,
}.get(value, defaultfunction)()

然而,在这种情况下,我相信你采取了错误的方法。 Python中的“思考”与其他语言略有不同,需要一段时间才能习惯“Python方式”。你似乎理解列表和字符串切片是好的。您可以利用这一点来简化您的Mastermind程序,如下所示。

import random

trial = 0
colour = ['B', 'G', 'Y', 'P', 'R']

pegs = [random.choice(colour), random.choice(colour), random.choice(colour), random.choice(colour)]
guess_pegs = ['?', '?', '?', '?']

def main():
    print "Generating a code"
    print pegs

    guess()    
    print "Congratulations! It took you %d tries to crack the code!\n" % trial
    print "The code was: " + ', '.join(pegs) 

def guess():
    global trial
    global pegs
    global guess_pegs

    entry_list = ['?', '?', '?', '?']

    while pegs != guess_pegs :
        entry_list = raw_input("What is your guess? ").split(' ')

        trial = trial + 1

        if len(pegs) == len(entry_list) :
            for i in range(0,4) :
                if(entry_list[i] == pegs[i]) :
                    guess_pegs[i] = entry_list[i]
        print guess_pegs                    
        guess()

# Kick things off
main()

答案 2 :(得分:0)

经验丰富的python程序员会写: -

elif a == peg2 or a == peg3 or a == peg4:

如: -

elif a in (peg2, peg3, peg4):