我对python还是很陌生,每当我运行脚本时,前两个函数humanPlay()
和computerPlay()
都会被调用两次。我不要这个。
当我在setRules()
函数中注释掉draw变量时,我注意到程序正常运行。我不确定f"{computerPlay()}"
和f"{humanPlay()}"
为什么不打印返回的值。
我还验证了py文件未命名为Random
是为了防止模块自行导入。
import random
Player = "Player"
Computer = "Computer"
options = ["ROCK","PAPER","SCISSORS"]
def main():
def humanPlay():
response = input("Make a selection between Rock, Paper, Scissors: ", )
response.upper()
if response.upper() in options:
print(response.upper())
return response.upper()
else:
print(f"{response.upper()}, is not a valid selection")
return humanPlay()
def computerPlay():
print(random.choice(options))
return random.choice(options)
def setRules():
rockWin = "You Win! ROCK beats SCISSORS "
paperWin = "You Win! PAPER beats ROCK "
scissorsWin = "You Win! SCISSORS beats PAPER "
draw = print("It's a Draw!, computer selected", f"{computerPlay()}", "and you selected", f"{humanPlay()}")
#loser = print(f"You Lose! {humanPlay()}", f"can't beat {computerPlay()}")
humanPlay()
computerPlay()
setRules()
main()
理想情况下,draw变量应等于以下内容:
It's a Draw!, computer selected ROCK and you selected ROCK
注意:我仍然必须为程序编写逻辑,以了解Rock,Paper和剪刀之间的区别。
现在我只希望返回正确的值,而不是整个函数。
答案 0 :(得分:1)
main
中被两次调用
main
的底部setRules
main
main
调用函数,并将值returned
存储为变量,以供在setRules
中使用rock
,paper
或scissor
之间的赢家Player
,Computer
和options
是全球性的。def humanPlay() -> str:
response = input("Make a selection between Rock, Paper, Scissors: ", )
response = response.upper()
if response in options:
print(response)
return response
else:
print(f"{response}, is not a valid selection")
return humanPlay()
def computerPlay() -> str:
comp_choice = random.choice(options)
print(comp_choice)
return comp_choice
def setRules(comp_choice: str, human_choice: str):
rockWin = "You Win! ROCK beats SCISSORS "
paperWin = "You Win! PAPER beats ROCK "
scissorsWin = "You Win! SCISSORS beats PAPER "
draw = print("It's a Draw!, computer selected", f"{comp_choice}", "and you selected", f"{human_choice}")
#loser = print(f"You Lose! {human_choice}", f"can't beat {comp_choice}")
Player = "Player"
Computer = "Computer"
options = ["ROCK","PAPER","SCISSORS"]
def main():
h_choice = humanPlay()
c_choice = computerPlay()
setRules(c_choice, h_choice)
main()
答案 1 :(得分:0)
问题在于您对f字符串插值的解释以及一般情况下的函数调用。
当您拥有some_func()
时,它将调用该函数(即其中的代码将被执行)。
f字符串插值的工作原理是,无论其内部是什么,它都会被执行并将其结果转换为字符串。
因此,要修复(此方面的)代码,应将humanPlay()
和computerPlay()
的结果收集到某个变量中,例如human_move = humanPlay()
,然后将该值传递给setRules()
(还要确保setRules()
确实接受该值)。
答案 2 :(得分:-1)
您看到humanPlay()
和computerPlay()
被两次呼叫的原因是因为您!
在setRules中打印它们时:
draw = print("It's a Draw!, computer selected", f"{computerPlay()}", "and you selected", f"{humanPlay()}")
您没有得到先前的结果,而是再次调用它们。
我会将humanPlay和computerPlay的返回值分配给某些变量,并将其传递给setRules。
即
import random
Player = "Player"
Computer = "Computer"
options = ["ROCK","PAPER","SCISSORS"]
def main():
def humanPlay():
response = input("Make a selection between Rock, Paper, Scissors: ", )
response.upper()
if response.upper() in options:
print(response.upper())
return response.upper()
else:
print(f"{response.upper()}, is not a valid selection")
return humanPlay()
def computerPlay():
choice = random.choice(options)
return choice
def setRules(humanResult, computerResult):
rockWin = "You Win! ROCK beats SCISSORS "
paperWin = "You Win! PAPER beats ROCK "
scissorsWin = "You Win! SCISSORS beats PAPER "
print("It's a Draw!, computer selected", f"{computerResult}", "and you selected", f"{humanResult}")
#loser = print(f"You Lose! {humanResult}", f"can't beat {computerResult}")
hResult = humanPlay()
cResult = computerPlay()
setRules(hResult, cResult)
main()