![](/img/trans.png)
[英]How can I make my code for a game of Rock, Paper, and scissors less redundant?
[英]How can I add another round to my Rock Paper Scissors game
我目前正在嘗試學習 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}') )
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.