这里的第一次海报。
我对Python比较陌生,我可能在业余时间编写了大约2-3个月的编码。为明年的进一步研究做准备。
我到处寻找解决方案,但似乎并不存在。如果不是这样,请道歉。
我正在努力学习如何像计算机科学家Python 3"一样思考。我已经对以下一组代码感到不安:
def play_once(human_plays_first):
"""
Must play one round of the game. If the parameter
is True, the human gets to play first, else the
computer gets to play first. When the round ends,
the return value of the function is one of
-1 (human wins), 0 (game drawn), 1 (computer wins).
"""
# This is all dummy scaffolding code right at the moment...
import random # See Modules chapter ...
rng = random.Random()
# Pick a random result between -1 and 1.
result = rng.randrange(-1,2)
print("Human plays first={0}, winner={1} "
.format(human_plays_first, result))
return result
def play():
"""initiates game and keeps track of score"""
humanscore = 0
computerscore = 0
draws = 0
total = 0
while True:
who_plays_first = input("Human plays? Yes or No")
if who_plays_first == 'Yes':
who_plays_first = True
result = play_once(who_plays_first)
if result == -1:
humanscore += 1
if result == 1:
computerscore += 1
if result == 0:
draws += 1
total = humanscore + computerscore + draws
PercentageComp = (computerscore / total) * 100
PercentageComp = round(PercentageComp, 2)
PercentageHuman = (humanscore / total) * 100
PercentageHuman = round(PercentageHuman, 2)
PercentageDraw = (draws / total) * 100
PercentageDraw = round(PercentageDraw, 2)
print("Scores are (human, computer and draws):" + str(humanscore) + ","+ str(computerscore) + ","+ str(draws) )
print("Computers have won: " + str(PercentageComp) + "%")
print("Human has one: " + str(PercentageHuman) + "%")
print("Draws have occured: "+ str(PercentageDraw) + "%")
response = input("Play again? (Yes or No)")
if response != "yes" and response !="Yes":
break
print("Goodbye!")
播放()
我的问题在于,无论是否将True传递给函数play_once(),它似乎都没有任何区别。游戏似乎也一样。我不确定我在这里失踪了什么。我也不确定游戏应该如何反应,似乎无论谁先行所有计算机都会随机选择一个数字来选择胜利者?
我认为我在这里肯定不见了,或者为什么它甚至会问谁想先走?
由于