重复游戏 - Python 石头、纸、剪刀 [英] Repeat Game - Python Rock, Paper, Scissors

查看:30
本文介绍了重复游戏 - Python 石头、纸、剪刀的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果之前有人问过这个问题的一个版本,我们深表歉意.我浏览了一遍,但找不到真正解决我问题的内容.

Apologies if a version of this question has been asked before. I looked through but couldn't find something that really addressed my problem.

我今天开始学习 Python 并尝试构建一个简单的石头剪刀布游戏.

I started learning python today and have tried to build a simple Rock, Paper, Scissors game.

我有以下代码并且运行良好:

I have the following code and it works pretty well:

import random

choices = ["rock", "paper", "scissors"]
player_move = input("Enter your move... ").lower()
cpu_move = random.choice(choices)
play = True
result_1 = ("Computer: " + cpu_move)
result_2 = ("You :" + player_move)
player_counter = 0
cpu_counter = 0
while play == True:
    print(result_2)
    print(result_1)
    if  cpu_move == "rock" and player_move == "paper" or cpu_move == "paper" and player_move == "scissors" or cpu_move == "scissors" and player_move == "rock":
        print("You win!")
        player_counter =+ 1
        print("Computer Score: " + str(cpu_counter))
        print("Your Score: " + str(player_counter))
        play = False
    elif cpu_move == "rock" and player_move == "scissors" or cpu_move == "paper" and player_move == "rock" or cpu_move == "scissors" and player_move == "paper":
        print("You lose!")
        cpu_counter += 1
        print("Computer Score: " + str(cpu_counter))
        print("Your Score: " + str(player_counter))
        play = False
    else:
        print("It's a tie!")
        play = True

但是,我不知道如何做以下两件事:

However, I can't work out how to do the following two things:

1) 如果游戏平局,则允许用户输入新的移动.2) 无论结果如何,都重复游戏,保持分数计数器运行.

1) allow the user to enter a new move if the game is tied. 2) repeat the game regardless of outcome buy keep the score counter going.

任何帮助将不胜感激!

谢谢,杰森

推荐答案

如果你移动了这一行

player_move = input("Enter your move... ").lower()

在while循环中,你应该能够同时

inside the while loop, you should be able to both

  1. 如果游戏平局,则允许用户输入新的移动,并且
  2. 无论结果如何,都要重复游戏,但要保持计分器运行.

当然,您还需要移动分配cpu_moveplayer_moveresult_1result_2 的代码> 在 while 循环中也是如此.

Of course, you'd also need to move the code assigning cpu_move, player_move, result_1 and result_2 inside the while loop as well.

从概念上讲,您可以将 while 循环之外的所有内容视为游戏的设置,而将其中的所有内容视为单个回合中发生的事情.

Conceptually, you can think of everything outside of the while loop as setup for the game, and everything inside it as what happens on a single turn.

这篇关于重复游戏 - Python 石头、纸、剪刀的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆