如何使用if语句检查函数内部的变量?

时间:2018-07-25 21:19:05

标签: python python-3.x

我正在制作基于文本的黑客游戏。如何使用if语句检查函数内部的变量?

错误:

Traceback (most recent call last):
  File "C:\Users\user\Desktop\Python Projects\TextHackingGame\Start.py", line 22, in <module>
    if check == "start":
NameError: name 'check' is not defined

代码:

import time
import string
import os
import sys

print("-------------------------")
print("Welcome to [NAME_HERE]")
print("By ***************")
print("Copyright 2018")
print("-------------------------")
time.sleep(0.10)
os.system("cls")

global Check
global check

def StartOrChange():
    print("Type 'Start' or 'Changelog'.")
    Check = input("> ")
    check = Check.lower

if check == "start":
    pass
elif check == "changelog":
    pass
else:
    print("Please enter 'Start' or 'Changelog'")

1 个答案:

答案 0 :(得分:0)

声明变量时,请使用check = 0Check = 0,在函数StartOrChange()中使用

def StartOrChange():
     global Check
     global check

     print("Type 'Start' or 'Changelog'.")
     Check = input("> ")
     print (Check) 
     check = Check.lower() 

https://docs.python.org/3/faq/programming.html