我正在 Replit 上制作一個賺錢的專案,而不是在現實生活中。我希望 'wheat_seads'、'melon_seeds'、'pumpkin_seeds'、'wheat'、'melon'、'pumpkin' 在函式內外可用,并在不同的函式中進行更改。我試圖將這些變數放入和取出“farm_command”以消除“語法錯誤”,但它不起作用。如果可以的話,改進這段代碼......
from replit import db
#This function is when 'help' command is run
def help_command():
print("So you need help...")
print("I am happy to help!")
print("1. 'help'")
print("2. 'farm'")
print("3. 'harvest'")
#This functon is when 'farm' command is run
def farm_command():
global wheat_seeds = 1
global melon_seeds = 1
global pumpkin_seeds = 1
global wheat = 0
global melon = 0
global pumpkin = 0
print("What seed do you want to farm?")
farm_seed = input("Seed: ")
farm_seed = farm_seed.lower()
if farm_seed == "wheat_seeds":
print("You planted " wheat_seeds " wheat seed/s")
wheat_seeds -= wheat_seeds
#The user's cash amount
db["User"] = {
"wallet":0,
"bank":0
}
#This is the users inventory
inventory = [wheat_seeds, melon_seeds, pumpkin_seeds, wheat, melon, pumpkin]
#This is prompted to ask the user what command should be run
command = input("Command: ")
#This checks which command is inputted and runs the respective command
if command == "help":
help_command()
elif command == "farm":
farm_command()
uj5u.com熱心網友回復:
定義全域變數時不能直接賦值。像這樣嘗試:
全球wheat_seeds 小麥
_seeds = 1
等等...
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/390176.html
上一篇:根據邏輯標準將變數合二為一
