Python索引不返回预期值

时间:2016-10-14 23:43:48

标签: python

我正在尝试从史密斯先生那里返回值M,但每当我在下面运行我的代码时,它返回''

// :id will match anything after the / in the url, and set it as the req.params.id value
app.get('/:id', function (req, res) {
  console.log("hi you're about to match url id to database id");
  //get a prostgres client from the connection pool
  pg.connect(connectionString, (err, client, done) => {
    //handle connection errors
    if (err) {
      done();
      console.log(err);
      return res.status(500).json({ success: false, data: err });
    }
    //match id in database to id from query in url
    const query = client.query("SELECT * FROM items WHERE id =" + req.params.id);
    console.log("successfully matched database id value to id value in url");

    console.log(req.query);
    res.redirect(req.query);
  });
});

3 个答案:

答案 0 :(得分:2)

python 3

   name = input("Please input your name: ")
    c=name.strip()[0]
    if c.isalpha():
        print(c)

python 2:

>>> name = raw_input("Please input your name: ")
Please input your name:   Hisham
>>> c=name.strip()[0]
>>> if c.isalpha():
    print c

输出py3:

Python 3.5.2 (default, Dec 2015, 13:05:11)
[GCC 4.8.2] on linux

Please input your name:   dsfgsdf
d

输出py2:

H

答案 1 :(得分:0)

我在这个提议的代码中看到了很多问题。

首先,input()将输入字符串计算为python表达式,而这不是您想要的,您需要使用raw_input()。 其次,如果输入包含','isalpha()将不会返回True。或空格,因此您需要找到另一个指标,例如,如果只允许使用字母字符,您可以这样使用isalpha()

name_nospaces = "".join(name.split())
if name_nospaces.isalpha():
    ...

然后,做name == name.find(' ')没有多大意义,只需要:

if name.find(' '):
    ...

答案 2 :(得分:0)

假设您必须找到名字的第一个字符....

def find_initial(name):
    i = 0
    initial_name_size = len(name)

    while True:
        name = name[i:]
        if name.isalpha():
            get_initial = name[0]
            return get_initial
        else:
            i = i+1
            if i >= initial_name_size:
                return 'There is no initial'


name = input("Please input your name: ")
print find_initial(name)