【问题标题】:Beginner Coding - Repeating a Rock, Paper, Scissors game using a while loop?初学者编码 - 使用 while 循环重复石头、纸、剪刀游戏?
【发布时间】:2015-06-17 15:46:36
【问题描述】:

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

`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() 将开始一个全新的游戏,但就像我提到的那样,它只是打印上一个游戏的结果。

任何帮助都会很棒,因为这是我第一次编码,也是第一次在这个网站上。谢谢。

【问题讨论】:

    标签: python python-2.7 loops while-loop


    【解决方案1】:

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

    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() 的调用)移动到单独的函数中,我们甚至可以推广其中的一些功能以减少重复代码的数量(你的目前的节目有很多)。

    【讨论】:

      猜你喜欢
      • 2021-03-12
      • 2020-05-22
      • 2015-05-16
      • 2013-12-09
      • 1970-01-01
      • 2019-02-23
      • 2015-01-15
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多