我的代码:
import random
options = ['Rock', 'Paper', 'Scissors']
gamecontrol = True
player_turn = ' '
computer_turn = ' '
def random_choose(options, player_turn, computer_turn):
player_turn = input('Enter your decision -> Rock,Paper,Scissors')
computer_turn = random.choice(options)
def win_check(player_turn, computer_turn):
if (player_turn == 'Rock' and computer_turn == 'Rock') or (player_turn == 'Paper' and computer_turn == 'Paper') \
or (player_turn == 'Scissors' and computer_turn == 'Scissors'):
print('DRAW!')
elif (player_turn == 'Rock' and computer_turn == 'Scissors') or (player_turn == 'Scissors' and computer_turn ==
'Paper') or (
player_turn == 'Paper' and computer_turn == 'Rock'):
print('PLAYER WON!')
elif (player_turn == 'Scissors' and computer_turn == 'Rock') or (player_turn == 'Paper' and computer_turn ==
'Scissors') or (
player_turn == 'Rock' and computer_turn == 'Paper'):
print('COMPUTER WON!')
while gamecontrol:
print('WELCOME TO THE ROCK & PAPER & SCISSORS GAME!')
random_choose(options, player_turn, computer_turn)
print(f"Player's choice: {player_turn}\nComputer's turn: {computer_turn}")
win_check(player_turn, computer_turn)
control = input('Do you want to play again?')
if control == 'y':
gamecontrol = True
else:
gamecontrol = False我试图写一个简单的岩石,纸,剪刀游戏,但当我试图开始游戏时,我得到了这样的结果:
WELCOME TO THE ROCK & PAPER & SCISSORS GAME!
Enter your decision -> Rock,Paper,ScissorsPaper
Player's choice:
Computer's turn:
Do you want to play again?在这里我看不出决定和谁赢了。能帮我一下吗?
发布于 2019-12-22 12:46:40
你的范围有问题。
在函数中定义与外部作用域中相同的局部变量,但它们并不相同。
k = 22
def func():
k = 33 # innner scope variable
print(id(k), k)
print(id(k),k) # global scope variable
func()
print(id(k),k) # global scope variable输出:
140235284185312 22 # different id's == different variables
140235284185664 33
140235284185312 22 # unmodified outer scope variable不要使用全局变量,而是从函数中返回值。我还优化了“胜利”检查:
import random
options = ['Rock', 'Paper', 'Scissors']
gamecontrol = True
def random_choose(options):
player_turn = input('Enter your decision -> Rock,Paper,Scissors')
return player_turn, random.choice(options)
def win_check(player_turn, computer_turn):
if player_turn == computer_turn:
print('DRAW!')
# it is better to compare against tuples here
elif (player_turn, computer_turn) in { ('Rock', 'Scissors'),
('Scissors','Paper'),
('Paper', 'Rock') }:
print('PLAYER WON!')
# not a draw, not player won - only computer won remains
else:
print('COMPUTER WON!')
while gamecontrol:
print('WELCOME TO THE ROCK & PAPER & SCISSORS GAME!')
player_turn ,computer_turn = random_choose(options)
print(f"Player's choice: {player_turn}\nComputer's turn: {computer_turn}")
win_check(player_turn, computer_turn)
control = input('Do you want to play again?')
if control == 'y':
gamecontrol = True
else:
gamecontrol = False输出:
WELCOME TO THE ROCK & PAPER & SCISSORS GAME!
Enter your decision -> Rock,Paper,ScissorsRock
Player's choice: Rock
Computer's turn: Paper
COMPUTER WON!
Do you want to play again?NHTH
发布于 2019-12-22 12:47:09
您正在用函数变量跟踪全局变量。要么删除它们,然后使用global关键字
def random_choose(options):
global player_turn
global computer_turn
player_turn = input('Enter your decision -> Rock,Paper,Scissors')
computer_turn = random.choice(options)
def win_check():
global player_turn
global computer_turn
if (player_turn == 'Rock' and computer_turn == 'Rock') or (player_turn == 'Paper' and computer_turn == 'Paper') or (player_turn == 'Scissors' and computer_turn == 'Scissors'):
print('DRAW!')
elif (player_turn == 'Rock' and computer_turn == 'Scissors') or (player_turn == 'Scissors' and computer_turn == 'Paper') or (player_turn == 'Paper' and computer_turn == 'Rock'):
print('PLAYER WON!')
elif (player_turn == 'Scissors' and computer_turn == 'Rock') or (player_turn == 'Paper' and computer_turn == 'Scissors') or (player_turn == 'Rock' and computer_turn == 'Paper'):
print('COMPUTER WON!')
while gamecontrol:
print('WELCOME TO THE ROCK & PAPER & SCISSORS GAME!')
random_choose(options)
print(f"Player's choice: {player_turn}\nComputer's turn: {computer_turn}")
win_check()
control = input('Do you want to play again?')
if control == 'y':
gamecontrol = True
else:
gamecontrol = False或完全删除它们并从random_choose返回值。
import random
options = ['Rock', 'Paper', 'Scissors']
gamecontrol = True
def random_choose(options):
player_turn = input('Enter your decision -> Rock,Paper,Scissors')
computer_turn = random.choice(options)
return player_turn, computer_turn
def win_check(player_turn, computer_turn):
if (player_turn == 'Rock' and computer_turn == 'Rock') or (player_turn == 'Paper' and computer_turn == 'Paper') or (player_turn == 'Scissors' and computer_turn == 'Scissors'):
print('DRAW!')
elif (player_turn == 'Rock' and computer_turn == 'Scissors') or (player_turn == 'Scissors' and computer_turn == 'Paper') or (player_turn == 'Paper' and computer_turn == 'Rock'):
print('PLAYER WON!')
elif (player_turn == 'Scissors' and computer_turn == 'Rock') or (player_turn == 'Paper' and computer_turn == 'Scissors') or (player_turn == 'Rock' and computer_turn == 'Paper'):
print('COMPUTER WON!')
while gamecontrol:
print('WELCOME TO THE ROCK & PAPER & SCISSORS GAME!')
choices = random_choose(options)
print(f"Player's choice: {choices[0]}\nComputer's turn: {choices[1]}")
win_check(choices[0], choices[1])
control = input('Do you want to play again?')
if control == 'y':
gamecontrol = True
else:
gamecontrol = Falsehttps://stackoverflow.com/questions/59444252
复制相似问题