【问题标题】:How do I make this piece of Python code work?如何使这段 Python 代码工作?
【发布时间】:2022-01-23 14:14:30
【问题描述】:

我正在 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()

【问题讨论】:

  • 请将 SyntaxError 的文本添加到您的帖子中
  • @Expurple global wheat_seeds = 1 是语法错误,但不幸的是代码还有很多问题。
  • 目前,想要帮助您的人需要猜测错误是什么,或者下载并运行您的代码。我会更容易只看错误。请粘贴完整的错误文本,例如这篇文章中的stackoverflow.com/questions/70447672/…

标签: python variables syntax-error


【解决方案1】:

定义全局变量时不能直接赋值。 试试这样:

全球小麦种子
小麦种子 = 1
等等……

【讨论】:

    猜你喜欢
    • 2013-05-04
    • 2011-05-16
    • 2014-05-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-04-09
    相关资源
    最近更新 更多