字符串检查器,无效输入-Python

时间:2018-06-28 21:50:22

标签: python

当我按Enter(空白输入)时,它是无效输入,因此不会打印,请勿将用户名留空

def strchecker(question):
    valid=False
    while not valid:
        user_Name = input(question)
        if user_Name!="":
            valid=True
            return user_name
        else:
            print("Do no leave username blank")

print("*************Welcome to the Te Reo Maori Quiz***************")
user_Name = str(input("Please enter your username")) 

1 个答案:

答案 0 :(得分:1)

您实际上并未调用该函数;这可能就是为什么它不起作用。试试这个:

let arr = [SaveItem(name: "name1"),SaveItem(name: "name2")]

do {

    // write

    var data  = try JSONEncoder().encode(arr)

    UserDefaults.standard.set(data, forKey: "Key")

    // read

    let content = UserDefaults.standard.data(forKey: "Key")

    let saved = try JSONDecoder().decode([SaveItem].self, from: content!)

    print(saved)

 }
 catch {

      print(error)
  }

这应该有效。