python - 如何在我的石头剪刀布游戏中添加另一轮
问题描述
我目前正在尝试学习 Python 并从事石头剪刀布游戏。它很简单,由 3 轮组成。大多数事情都运行良好,但我现在唯一的问题是,如果一轮是平局,我不能再添加一轮。
from random import randint
options = ['rock', 'paper', 'scissors']
players = 0
computers = 0
computer = options[randint(0,2)]
#introduction
print("Welcome to rock paper scissors")
print("The game is fairly simple.\n- Rock beats Scissors\n- Scissors beats Paper \n- Paper beats Rock")
start = input("To start the game type 'Start' or 's' ")
if start != 's':
print("Ok")
playerplay = False
else:
rounds = 3
playerplay = True
for loop in range(rounds):
player = input("Rock, Paper, Scissors? ").lower()
if player == computer:
print("It's a Tie!")
rounds += 1
elif player == options[0]:
if computer == options[1]:
print(computer, "covers", player)
print("Player lost, 1 point for the computer")
computers += 1
else:
print(player, 'smashes', computer)
print("Player wins, 1 point for the player")
players += 1
elif player == options[1]:
if computer == options[2]:
print(computer, "cuts", player)
print("Player lost, 1 point for the computer")
computers += 1
else:
print(player, 'covers', computer)
print("Player wins, 1 point for the player")
players += 1
elif player == options[2]:
if computer == options[1]:
print(computer, "smashes", player)
print("Player lost, 1 point for the computer")
computers += 1
else:
print(player, "cuts", computer)
print("Player wins, 1 point for the player")
players += 1
if playerplay:
print((f'Player {players}\nComputer {computers}') )
解决方案
我建议使用while
回合:如果是平局则添加+1,并在每回合后减少-1并使游戏运行直到回合为+ive
from random import randint
options = ['rock', 'paper', 'scissors']
players = 0
computers = 0
computer = options[randint(0,2)]
#introduction
print("Welcome to rock paper scissors")
print("The game is fairly simple.\n- Rock beats Scissors\n- Scissors beats Paper \n- Paper beats Rock")
start = input("To start the game type 'Start' or 's' ")
if start != 's':
print("Ok")
playerplay = False
else:
rounds = 3
playerplay = True
# for loop in range(rounds):
while rounds: #use while loop
player = input("Rock, Paper, Scissors? ").lower()
if player == computer:
print("It's a Tie!")
rounds += 1# increase rounds left
elif player == options[0]:
if computer == options[1]:
print(computer, "covers", player)
print("Player lost, 1 point for the computer")
computers += 1
else:
print(player, 'smashes', computer)
print("Player wins, 1 point for the player")
players += 1
elif player == options[1]:
if computer == options[2]:
print(computer, "cuts", player)
print("Player lost, 1 point for the computer")
computers += 1
else:
print(player, 'covers', computer)
print("Player wins, 1 point for the player")
players += 1
elif player == options[2]:
if computer == options[1]:
print(computer, "smashes", player)
print("Player lost, 1 point for the computer")
computers += 1
else:
print(player, "cuts", computer)
print("Player wins, 1 point for the player")
players += 1
rounds-=1 # dcrease the rounds left
if playerplay:
print((f'Player {players}\nComputer {computers}') )
推荐阅读
- bazel - 有任何 Bazel 文档可以帮助从旧版本迁移到新版本吗?
- python - 尝试抓取网站时连接被拒绝错误
- javascript - ReferenceError:文档未在 javascript 中定义
- c - 将制表符分隔的数据读取到C中的数组
- reactjs - 由于 webpack gl 错误,下一次构建失败
- python - 每个 subplotSpec 的标题(子图中的子图)
- node.js - 带有 localhost:3000 的 http-server 给出 ERR_INVALID_REDIRECT
- javascript - 找不到模块“connect-session-knex”的声明文件
- javascript - 如何使 D3 碰撞力仅适用于 X
- c# - 在 Windows 窗体初始化 (MVC) 时触发事件/加载数据