我正在做一个小脚本,该脚本从用户那里接收名称,然后搜索名称,如果找到则打印其姓名,否则打印“未找到”。
def std_record():
print("1. Enter data.")
print("2. Search student")
user_input = int(input("Enter Choice: "))
if user_input == 1:
name = input("Whats your name: ")
elif user_input == 2:
search = input("Enter keyword: ")
if search in name:
print("data found: " + name +".")
else:
print("not found")
while True:
std_record()
UnboundLocalError:分配前已引用本地变量“名称”
答案 0 :(得分:0)
尝试初始化名称变量:
name = None
def std_record():
print("1. Enter data.")
print("2. Search student")
user_input = int(input("Enter Choice: "))
if user_input == 1:
name = input("Whats your name: ")
elif user_input == 2:
search = input("Enter keyword: ")
if name:
print("data found: " + name +".")
else:
print("not found")
while True:
std_record()
答案 1 :(得分:0)
使用字符串之前,您需要对其进行初始化,要么为空字符串,要么为空字符串。
so name =“” 或名称=无