【发布时间】:2021-03-27 12:06:06
【问题描述】:
关于这段代码的2个问题。代码可以工作,但是关于 is_win 函数的代码中有一些我不明白的地方。
问题一——定义函数is_win时,是否在if语句中调用它,在def play函数内?
问题 2 - 如果是,我不明白为什么需要参数用户和计算机来执行计算,而不是玩家和对手。通常,当您调用函数时,信息由函数中定义的参数转换。但在这种情况下,它似乎反过来工作。谁能解释一下这是为什么?
谢谢。
import random
def play():
user = input ("What's your choice? 'R' for rock, 'P' for paper, 'S' for scissors: ")
computer = random.choice(["r", "p", "s"])
print (f"Computer chose {computer}")
if user == computer:
return ("You tie")
if is_win(user,computer):
return ("You won")
return "You lost"
def is_win(player,opponent):
if (player == "r" and opponent == "s") or (player == "s" and opponent == "p") or (player == "p" and opponent == "r"):
return True
print (play())
【问题讨论】:
标签: function parameter-passing