TypeError:期望的整数参数,浮点数IN LIST PYTHON

时间:2015-12-14 16:38:17

标签: python list loops

由于某种原因,行“new1 = [chr(x)for x in new]”不能采用浮点数。我将“新”列表中的所有ASCII整数转换回带有字符的列表,根据它们各自的ASCII号,该列表将保存为new1。

if de == "d":
   num = input("input ur number: ")
   key = int(input("Decryption key?"))
   num = [int(x) for x in num.split()]
   new = [x /key for x in num]
   new1 = [chr(x) for x in new]

这是错误:

- Traceback(最近一次调用最后一次):   文件“C:\ Python34 \ Encryptiontrue.py”,第23行,in     new1 = [chr(x)for x in new]

- 文件“C:\ Python34 \ Encryptiontrue.py”,第23行,in     new1 = [chr(x)for x in new]

- TypeError:期望的整数参数,浮点数

完整代码:

de = input("Decrypt or encrypt?: ")

if de == "e":
   word = input("input ur text: ")
   word = word.lower() 
   new = list(word)

   key = int(input("what number by?: "))

   new1 = [ord(x) * user for x in new]

   new2 = " ".join(str(x) for x in new1)
   print(new2)

if de == "d":
   num = input("input ur number: ")
   key = int(input("Decryption key?"))
   num = [int(x) for x in num.split()]
   new = [x /key for x in num]
   new1 = [chr(x) for x in new]

1 个答案:

答案 0 :(得分:1)

更改

    JTextField findtext = new JTextField();
    //findtext.setBounds(112,549, 63, 24);
    frame.add(findtext,BorderLayout.BEFORE_FIRST_LINE);

    findtext.addKeyListener(new KeyAdapter() {
        public void keyPressed(KeyEvent evt) {
            int code = evt.getKeyCode();

            if(code == KeyEvent.VK_ENTER){
                String find = findtext.getText().toLowerCase();
                tarea.requestFocusInWindow();
                //findtext.requestFocus();
                //findtext.requestFocusInWindow();
                if (find != null && find.length()> 0) {
                    Document document = tarea.getDocument();
                    int findLength = find.length();
                    try {
                         //int pos=0;
                         boolean found = false;
                        if (pos + findLength > document.getLength()) {
                            pos = 0;
                        }
                    while (pos + findLength <= document.getLength()) {
                        String match = document.getText(pos, findLength).toLowerCase();
                        if (match.equals(find)) {
                            found = true;
                            break;
                        }
                        pos++;
                    }
                    if (found) {
                        Rectangle viewRect = tarea.modelToView(pos);
                        tarea.scrollRectToVisible(viewRect);
                        tarea.setCaretPosition(pos + findLength);
                        tarea.moveCaretPosition(pos);
                        pos += findLength;
                        //Thread.sleep(2000);
                      //  findtext.requestFocusInWindow();

                    }

                } catch (Exception exp) {
                    exp.printStackTrace();
                }
            }

                 }//findtext.requestFocusInWindow();
        }
    });

    /*control back to textarea*/
    tarea.addKeyListener(new KeyAdapter() {
        public void keyPressed(KeyEvent evt) {
            int code = evt.getKeyCode();

            if(code == KeyEvent.VK_ENTER){
                String find = findtext.getText().toLowerCase();
                tarea.requestFocusInWindow();
                //findtext.requestFocus();
                //findtext.requestFocusInWindow();
                if (find != null && find.length()> 0) {
                    Document document = tarea.getDocument();
                    int findLength = find.length();
                    try {
                         //int pos=0;
                         boolean found = false;
                        if (pos + findLength > document.getLength()) {
                            pos = 0;
                        }
                    while (pos + findLength <= document.getLength()) {
                        String match = document.getText(pos, findLength).toLowerCase();
                        if (match.equals(find)) {
                            found = true;
                            break;
                        }
                        pos++;
                    }
                    if (found) {
                        Rectangle viewRect = tarea.modelToView(pos);
                        tarea.scrollRectToVisible(viewRect);
                        tarea.setCaretPosition(pos + findLength);
                        tarea.moveCaretPosition(pos);
                        pos += findLength;
                        //Thread.sleep(2000);
                      //  findtext.requestFocusInWindow();

                    }

                } catch (Exception exp) {
                    exp.printStackTrace();
                }
            }

                 }//findtext.requestFocusInWindow();
        }
    });


    /*search filed ends*/

new = [x / key for x in num]

在Python 3中,new = [x // key for x in num] 表示“真正的除法”,它总是返回一个浮点数。 /表示“地板划分”。如果两个参数都是整数,则Floor division将返回一个整数。有关详细信息,请参阅{{3}}。