如何根据hashmap的键和值检查两个输入?

时间:2016-12-02 22:49:17

标签: java hashmap

我刚刚学习HashMaps,并且刚刚使用它们编写了我的第一个程序。出于某种原因,我检查以确定我输入的输入是否与密钥匹配,并且它的相应值始终返回false。谁能告诉我为什么会这样?

import httplib2
import os
import oauth2client
from oauth2client import client, tools
import base64
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
from apiclient import errors, discovery

SCOPES = 'https://www.googleapis.com/auth/gmail.send'
CLIENT_SECRET_FILE = 'client_secret.json'
APPLICATION_NAME = 'Gmail API Python Send Email'

def get_credentials():
    home_dir = os.path.expanduser('~')
    credential_dir = os.path.join(home_dir, '.credentials')
    if not os.path.exists(credential_dir):
        os.makedirs(credential_dir)
    credential_path = os.path.join(credential_dir, 'gmail-python-email-send.json')
    store = oauth2client.file.Storage(credential_path)
    credentials = store.get()
    if not credentials or credentials.invalid:
        flow = client.flow_from_clientsecrets(CLIENT_SECRET_FILE, SCOPES)
        flow.user_agent = APPLICATION_NAME
        credentials = tools.run_flow(flow, store)
        print('Storing credentials to ' + credential_path)
    return credentials

def SendMessage(sender, to, subject, msgHtml, msgPlain):
    credentials = get_credentials()
    http = credentials.authorize(httplib2.Http())
    service = discovery.build('gmail', 'v1', http=http)
    message1 = CreateMessage(sender, to, subject, msgHtml, msgPlain)
    SendMessageInternal(service, "me", message1)

def SendMessageInternal(service, user_id, message):
    try:
        message = (service.users().messages().send(userId=user_id, body=message).execute())
        print('Message Id: %s' % message['id'])
        return message
    except errors.HttpError as error:
        print('An error occurred: %s' % error)

def CreateMessage(sender, to, subject, msgHtml, msgPlain):
    msg = MIMEMultipart('alternative')
    msg['Subject'] = subject
    msg['From'] = sender
    msg['To'] = to
    msg.attach(MIMEText(msgPlain, 'plain'))
    msg.attach(MIMEText(msgHtml, 'html'))
    raw = base64.urlsafe_b64encode(msg.as_bytes())
    raw = raw.decode()
    body = {'raw': raw}
    return body

def main():
    to = "to@address.com"
    sender = "from@address.com"
    subject = "subject"
    msgHtml = "Hi<br/>Html Email"
    msgPlain = "Hi\nPlain Email"
    SendMessage(sender, to, subject, msgHtml, msgPlain)

if __name__ == '__main__':
    main()

1 个答案:

答案 0 :(得分:1)

while循环中的代码存在两个问题,如下所述:

(1)keyboard.next()正在读取控制台输出文本,即阅读打印文本“密码”,因此将keyboard.next()替换为keyboard.nextLine();

(2)您没有处理else计数

tries条件

您可以使用内嵌注释参考以下代码:

while(b==false){
       System.out.print("Login: ");
       inputUsr=keyboard.nextLine();
       System.out.print("\nPassword: ");
       String inputPass=keyboard.nextLine();
       if(inputPass.equals(userPass.get(inputUsr))) {
           b=true;
       } else { 
          System.out.println("Either the username
            or password is incorrect. 
                      You have "+(3-tries)+" more attempts.");
                    tries++;
        }
        if(tries>3){
            System.exit(0);
        }
}