[英]My program that uses functions in a while loop does not keep updated values
我正在為 class 創建一個撿起棍子的游戲。我們的目標是創建一個 function 循環運行游戲的其他 4 個函數。
main():
while numb_sticks > 1:
func1()
func2()
func3()
func4()
else:
print("Player {} wins!")
我的問題是保留我從一個 function 更改到下一個的值。 我在我的函數之外聲明了 numb_sticks,還有起始玩家。
numb_sticks = 20
player = 1
我在整個函數中都有 print() 來跟蹤正在更改的值。 我已經確定,當 function 完成時,numb_stick 和 player 值將恢復到 function 之外的原始值。這實質上創建了一個無限循環,其中木棒的數量總是恢復到 20,而輪到始終是玩家 1。我是需要將這兩個變量用作我的函數的參數。 我不知道如何解決這個問題。 以下是我的全部代碼。 我在整個程序中添加了注釋來解釋我在做什么。
pick_up_sticks.py
import random
def not_quite_right(numb_sticks): #This function creates a chance to add sticks to the game.
global sticks_added #Declaring variables global to be changed across program.
global sticks_remaining
print("Initial numb_sticks nqr:", numb_sticks) #Testing change.
n = random.randint(1,10) #This and the next line set up the %30 chance for more sticks.
if n > 7:
sticks_added = random.randint(1,4) #This sets up a random amount of sticks between 1-4 to be added.
if numb_sticks + sticks_added <= 20: #Checks if cap is exceeded.
numb_sticks += sticks_added #Adding sticks to pile.
print(numb_sticks) #Testing change, will remove on final.
return
else:
sticks_added = 20 - numb_sticks #This is a small work around if the sticks added would have been more than 20.
numb_sticks = 20
return
else: #The following complets the 70% chance of no sticks being added.
sticks_added = 0
return
"""
---
"""
def take_sticks(player, numb_sticks):
global sticks_taken #Setting variables as global for use across program.
global sticks_remaining
print("Initial numb_sticks ts:", numb_sticks) #Test before change.
if numb_sticks > 1: #Checks if game is still going.
sticks_taken = input("Player {}, choose how many sticks you would like to take: ".format(player)) #Lets player choose stick amount.
try: #Next 4 lines check if the input is a number and restarts turn if it isn't.
int(sticks_taken)
except ValueError:
print("Invalid input! Please type a number.")
take_sticks(player, numb_sticks)
sticks_taken = int(sticks_taken) #Sets input as integer.
if sticks_taken <= 0 or sticks_taken >= 4: #Next 3 lines check if inputted number is legal move and restarts turn if it isn't.
print("You can only take 1, 2, or 3 sticks.")
take_sticks(player, numb_sticks)
elif sticks_taken < 4 and sticks_taken > 0: #Determines legal move.
numb_sticks -= sticks_taken #Sets sticks remaining by removing sticks taken from original 20 sticks.
print("Numb_sticks:", numb_sticks) #Test change.
if player == 1: #Next 5 lines changes players turn.
player = 2
print("Next turn is player", player,"'s turn.'") #Test player change.
return
else:
player = 1
return
return
else:
return
else:
return
def display_board(numb_sticks): #This function states how many sticks there are at the start of the turn.
print("There are", numb_sticks, " sticks on the board.")
def display_summary(player, sticks_taken, sticks_added, numb_sticks): #The following function simply displays the remaining sticks and the next players turn.
print("Player", player,"took", sticks_taken, "sticks, pictsie added", sticks_added, "sticks back, leaving", numb_sticks, "sticks.")
def main(): #Creating a loop for the game.
numb_sticks = 20 #Setting the number of sticks.
player = 1 #Sets player 1 to start.
while numb_sticks > 1:
display_board(numb_sticks)
take_sticks(player, numb_sticks)
not_quite_right(numb_sticks)
display_summary(player, sticks_taken, sticks_added, sticks_remaining)
else:
print("Player {} has won the game!".format(player)) #Declares a player the winner!
main() #Begins the game!
那么,您是否嘗試傳遞在 main() 中聲明為函數參數的全局值? 這樣你的全局值總是與函數相關聯。
def main():
numb_sticks = 20
player = 1
if numb_sticks > 1:
func1(numb_sticks, player)
func2(numb_sticks, player)
else:
print("Player {} wins!")
def func1(numb_sticks, player):
def func2(numb_sticks, player):
無限循環的原因是無論numb_sticks
遞減還是遞增, numb_sticks
都是一個臨時局部變量(好吧,它是一個參數,它也是一個臨時變量,就像局部變量一樣)。 一旦 function 到達該特定調用的末尾,臨時變量numb_sticks
(及其遞減值或遞增值)就會消失。
有很多方法可以解決這個問題。
我通過以下更改擺脫了無限循環:
修改了函數not_quite_right()
和take_sticks()
以便每次return
都更改為return numb_sticks
。 這會將局部變量的(增加或減少的值)返回到調用 function 的調用點。
接下來,我修改了main()
function 中while
循環的主體,如下所示:
display_board(numb_sticks) numb_sticks = take_sticks(player, numb_sticks) numb_sticks = not_quite_right(numb_sticks) display_summary(player, sticks_taken, sticks_added, sticks_remaining)
游戲以大約 5 步結束。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.