Key1是IS定义的,错误说它不是

时间:2015-12-14 12:15:50

标签: python

我正在运行我的代码,但它不起作用。但是,这不是问题。我定义了" key1"并且会出现错误

NameError: name 'key1' is not defined

如果有人能找到问题,我将不胜感激。我将在下面发布完整的代码。

alphabet = "abcdefghijklmnopqrstuvwxyzABCDEFHIJKLMNOPQRSTUVWXYZ0123456789"
choice = input("Would you like to encrypt or decrypt? [e/d]: ")
string = ''

if choice == "e":
    message = input("Please insert the message you would like to use: ")
    keyword = input("Please insert the keyword you would like to use: ")
    for A in message:
        if message in alphabet:
            message1 = (ord(message)) - 96
        for A in keyword:
            if keyword in alphabet:
                key1 = (ord(keyword)) - 96
    addition = key1 + message1
    string = (chr(addition))
    print (string)

1 个答案:

答案 0 :(得分:0)

如果keyword不在alphabet,那么当您到达addition = key1 + message1时,将无法定义key1=""。您可以通过在条件之前设置if keyword in alphabet: ... else: key1 = "" 或使用else语句来解决此问题:

message1

您也应该在上面的if声明中为 String driver = "com.mysql.jdbc.Driver"; String url = "jdbc:mysql://localhost:3306/game"+"?verifyServerCertificate=false"+"&useSSL=true"; String user = "root"; String pass = "password"; String sql = "SELECT username, password FROM player WHERE username = ? AND password = ?"; String username = "uname"; String password = "pword"; try { Class.forName(driver); Connection connection = DriverManager.getConnection(url, user, pass); PreparedStatement statement = connection.prepareStatement(sql); statement.setString(1, username); statement.setString(2, password); ResultSet resultSet = statement.executeQuery(); boolean loginSuccess = false; while (resultSet.next()) { loginSuccess = true; System.out.println("Congrats! You are logged in as " + resultSet.getString("username") + "."); } } catch (Exception e) { throw new RuntimeException("Login failed", e); } 执行此操作。