如何解决:“不同模块中的变量不起作用”

时间:2019-01-12 13:01:38

标签: python

代码在entryQuestions部分获得玩家的名字。当到达“ loadCasino”时,代码显示错误。

  

“追踪(最近一次通话结束):
  
中的文件“ main.py”,第38行   loadCasino()
  loadCasino中第34行的文件“ main.py”
  打印(“到达赌场。备用”,名称)
  NameError:名称“名称”未定义”

我的代码包含“ replit.clear()”函数。这样是否也可以清除变量,如果是这样,也许就是问题所在。

def entryQuestions():
  time.sleep(1)
  print("Please answer the following questions to log into the 
  Casino.")
  time.sleep(1)
  name = input("Please enter a Username: ")
  time.sleep(1)
  print("Information loaded.")
  time.sleep(2)
  print("Beginning transportation to Casino...")

def loadCasino():
  time.sleep(2)
  replit.clear()
  print("Transporting to Casino.")
  time.sleep(2)
  replit.clear()
  print("Transporting to Casino..")
  time.sleep(2)
  replit.clear()
  print("Transporting to Casino...")
  time.sleep(2)
  replit.clear()
  print("Arrived at Casino. Standby", name)

预期结果应为打印“到达赌场。备用,(在此命名)”的脚本。

该代码正在repl.it上使用,可以在here中找到:

import replit
import time

replit.clear()

def topBanner():
  replit.clear()
  print("======================================")
  print("Frenzy Casino - Made by @harrisoncopp")
  print("======================================")

def entryQuestions():
  time.sleep(1)
  print("Please fill in the details below to create, or login to, a new accont in order to play at the Casino.")

# REGISTER / LOGIN WILL GO HERE

def loadCasino():
  time.sleep(2)
  replit.clear()
  print("Transporting to Casino.")
  time.sleep(2)
  replit.clear()
  print("Transporting to Casino..")
  time.sleep(2)
  replit.clear()
  print("Transporting to Casino...")
  time.sleep(2)
  replit.clear()
  print("Arrived at Casino. Standby")

topBanner()
entryQuestions()
loadCasino()

1 个答案:

答案 0 :(得分:2)

您需要在name的本地范围内分配loadcasino()

我只是从输入请求中返回了name变量。并将其传递给loadcasion()函数。

import replit
import time

replit.clear()

def topBanner():
  replit.clear()
  print("======================================")
  print("Frenzy Casino - Made by @harrisoncopp")
  print("======================================")

def entryQuestions():
  time.sleep(1)
  print("Please answer the following questions to log into the Casino.")
  time.sleep(1)
  name = input("Please enter a Username: ")
  time.sleep(1)
  print("Information loaded.")
  time.sleep(2)
  print("Beginning transportation to Casino...")
  return name

def loadCasino():
  time.sleep(2)
  replit.clear()
  print("Transporting to Casino.")
  time.sleep(2)
  replit.clear()
  print("Transporting to Casino..")
  time.sleep(2)
  replit.clear()
  print("Transporting to Casino...")
  time.sleep(2)
  replit.clear()
  print("Arrived at Casino. Standby", name)

topBanner()
name = entryQuestions()
loadCasino(name)

我检查了您的repl.it代码,name变量下划线为红色。它告诉您尚未定义。您应该尝试并遵循编辑器的自动检查。