初学者编码 - 使用while循环重复摇滚,纸张,剪刀游戏?

时间:2015-04-12 06:17:04

标签: python python-2.7 loops while-loop

我是一个刚刚开始的基本编码员,我有这个任务,在游戏结束时,它询问用户是否想再次玩,他们输入Y或N.我的问题是什么时候他们打Y再玩,我的def游戏()打印出上一场比赛的第一场比赛的结果,而不是重复整场比赛。我的其余代码运行正常。 到目前为止,这是我的代码:

`def intro():
    print "Welcome to Rock, Paper, Scissors."
    print "This is a game between two opponents!"
    print "Have Fun!"
    print
def rematch():
    while 1:
        retry = raw_input("Would you like to play again? Y or N: ")
        if retry == 'Y':
            game() 
        else:
            ending()
            break
def ending():
    print "Thank you for playing!"
    print "Please come back and try again."
def game():
    if player1 == "p" and player2 == "r":
        print
        print "Player 1 wins"
        return 1
    elif player1 == "s" and player2 == "p":
        print
        print "Player 1 wins"
        return 1
    elif player1 == "r" and player2 == "s":
        print
        print "Player 1 wins"
        return 1
    elif player1 == "p" and player2 == "s":
        print
        print "Player 2 wins"
        return 2
    elif player1 == "s" and player2 == "r":
        print
        print "Player 2 wins"
        return 2
    elif player1 == "r" and player2 == "p":
        print
        print "Player 2 wins"
        return 2
    elif player1 == "p" and player2 == "p":
        print
        print "Tie"
        return 0
    elif player1 == "s" and player2 == "s":
        print
        print "Tie"
        return 0
    elif player1 == "r" and player2 == "r":
        print
        print "Tie"
        return 0
score1 = 0
score2 = 0
intro()
player1 = raw_input("Player 1 what is your choice, (R)ock, (P)aper, or (S)cissors? ").lower()
player2 = raw_input("Player 2 what is your choice, (R)ock, (P)aper, or (S)cissors? ").lower()
game1 = game()
if game1 == 1:
    score1 = score1 + 1
    print
    print "Player 1's score is ", score1
    print "Player 2's score is ", score2
elif game1 == 2:
    score2 = score2 + 1
    print
    print "Player 1's score is ", score1
    print "Player 2's score is ", score2
elif game1 == 0:
    print
    print "Player 1's score is ", score1
    print "Player 2's score is ", score2
    print
    print "Get ready for Round 2!"
    print
player3 = raw_input("Player 1 what is your choice, (R)ock, (P)aper, or (S)cissors? ").lower()
player4 = raw_input("Player 2 what is your choice, (R)ock, (P)aper, or (S)cissors? ").lower()
game2 = game()
if game2 == 1:
    score1 = score1 + 1
    print
    print "Player 1's score is ", score1
    print "Player 2's score is ", score2
elif game2 == 2:
    score2 = score2 + 1
    print
    print "Player 1's score is ", score1
    print "Player 2's score is ", score2
elif game2 == 0:
    print
    print "Player 1's score is ", score1
    print "Player 2's score is ", score2
    print
    print "Final Round!"
    print
player5 = raw_input("Player 1 what is your choice, (R)ock, (P)aper, or (S)cissors? ").lower()
player6 = raw_input("Player 2 what is your choice, (R)ock, (P)aper, or (S)cissors? ").lower()
game3 = game()
if game2 == 1:
    score1 = score1 + 1
    print
    print "Player 1's final score is ", score1
    print "Player 2's final score is ", score2
elif game2 == 2:
    score2 = score2 + 1
    print
    print "Player 1's final score is ", score1
    print "Player 2's final score is ", score2
elif game2 == 0:
    print
    print "Player 1's final score is ", score1
    print "Player 2's final score is ", score2 
rematch()`

现在就在这里:

`def rematch():
    while 1:
        retry = raw_input("Would you like to play again? Y or N: ")
        if retry == 'Y':
            game() 
        else:
            ending()
            break`

在我看来,我认为game()会开始一个全新的游戏,但就像我提到的那样,只打印上一场比赛的结果。

任何帮助都会很棒,因为这是我第一次编码以及第一次在本网站上进行编码。感谢。

1 个答案:

答案 0 :(得分:0)

也许这个简化的例子比关注当前程序的细节更有帮助:

def main():
    intro()
    while True:
        play_game()
        if not rematch():
            break
    ending()

def intro():
    print '\nWelcome!\n'

def ending():
    print '\nGood night!\n'

def play_game():
    p1 = int(raw_input('> ') or 0)
    p2 = int(raw_input('> ') or 0)
    winner = evaluate(p1, p2)
    print_result(winner)

def evaluate(p1, p2):
    if   p1 > p2: return 1
    elif p1 < p2: return 2
    else:         return 3

def print_result(p):
    print '\nWinner: player {}\n'.format(p)

def rematch():
    return raw_input('Continue? ').lower().startswith('y')

if __name__ == '__main__':
    main()

值得注意的几点:

  • 所有代码都驻留在函数内部。除最后两行外,没有任何内容在顶层执行。

  • main()函数只是编排事物而不关心细节。关键是要弄清楚如何在main()函数中的高级别表达您的游戏。在手头的例子中,我们一直在玩,直到不想要复赛。

  • 输出的打印与代码逻辑分开。请注意,例如,evaluate()函数获取数据并返回数据:它不会打印。如果我们一直遵循这个原则,我们也会将其他用户交互(调用raw_input())移动到单独的函数中,我们甚至可以推广其中一些功能以减少重复代码的数量(你当前的程序有很多)。