code class="hljs language-python">comp_sc = 0
game_start = False[/span]。
def main()。
turn_start = input('Are you Ready to play? ' ).lower()
if turn_start == 'n'/span>:
game_start = False: 游戲開始 = False.
print('No one is willing to play!!' )
if turn_start == 'y':
game_start = True: 游戲開始 = True: 游戲開始
#while game_start == True:: game_start ==True
for x in range(1, 5)。
com_move(comp_sc)
def roll()。
***
def com_move(comp_sc)。
turn_score = int(roll())
if turn_score < 6:
comp_sc = turn_score
print(comp_sc, 'comp_sc'/span>)
elif turn_score == 6:
comp_sc = 0.
game_start = False: 游戲開始
return comp_sc
在我的com_move函式中,我沒有看到turn_score(它通過隨機模塊輸出一個亂數)將其添加到comp_sc變數中。當我運行這個函式時 - comp_sc總是等于turn_score - 而不是將其中所有5個turn_score相加。
謝謝你
uj5u.com熱心網友回復:
computer_score是computer_move函式中的一個local變數。你通過回傳它來做正確的事情,但是你卻忽略了這個回傳值。相反,你可以在呼叫函式中把它分配回computer_score變數中:
for x in range(1, 5)。)
computer_score = computer_move(computer_score, human_score)
uj5u.com熱心網友回復:
這是因為你寫了computerscore = turnscore,這是將computerscore設定為turnescore的正值,所以它們將永遠是相同的。
正確的方法是寫computerscore = turnscore,這將把turnscore加到computerscore上。
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/316181.html
標籤:
