[英]why am i getting NameError: global name 'spacing' is not defined
I'm getting a NameError: global name 'spacing' is not defined for and im not sure why 我得到一个NameError:全局名称'spacing'没有定义,我不知道为什么
def main():
print("Project One\n")
print("To encrypt message enter: 1")
print("To decrypt messsge enter: 2\n")
pick = int(input())
if pick == 1:
print("Encrypting!")
spacing()
elif pick == 2:
print("Decrypting!")
else:
print("Try again")
if __name__ == '__main__':
main()
def spacing():
text = raw_input("Please Enter the message to encrypt: ").upper()
print(text)
key = raw_input("Please enter a key to encrypt").upper()
print(key)
space_ignore = ""
j = 0
for i in range(len(text)):
if ord(text[i]) == 32:
space_ignore += " "
else:
if j < len(key):
space_ignore += key[j]
j += 1
else:
j = 0
space_ignore += key[j]
j += 1
print(space_ignore)
return text, space_ignore
Python defines are evaluated in-order relative to other statements. Python定义是相对于其他语句按顺序计算的。 By the time
main()
runs, spacing()
has not yet been defined since control has not yet reached def spacing(): ...
. 到
main()
运行时,还没有定义spacing()
因为控件尚未达到def spacing(): ...
Move your if __name__ == 'main':
block to below the definition of spacing
. 将
if __name__ == 'main':
块移动到spacing
定义下方。
Put spacing()
definition above main
definition and the last thing you need to place is this: 将
spacing()
定义放在main
定义之上,最后需要放置的是:
if __name__ == '__main__':
main()
python is a scripting language,would do “line by line” or easy say:from top to end. python是一种脚本语言,可以“逐行”或简单地说:从上到下。 make the spacing before main.
在主要之前做出间距。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.