小编Sc4*_*c4r的帖子

PEP8:延续线过度缩进以进行视觉缩进

我有这行代码超过了这一行,当测试pep8错误时,我得到:行太长了.因此,为了尝试修复此问题,我使用了斜杠('\'),但后来我得到了延伸线,用于缩进视觉缩进.我该怎么做才能解决这个问题?

在此输入图像描述

我试过的事情:

if first_index < 0 or second_index > \
   self._number_of_plates - 1:
    raise ValueError

continuation line over-indented for visual indent

if first_index < 0 \ 
   or second_index > \
   self._number_of_plates - 1:
    raise ValueError

continuation line over-indented for visual indent

if first_index < 0 or \
   second_index > self._number_of_plates - 1:
    raise ValueError

continuation line over-indented for visual indent

if first_index \
   < 0 or second_index \
   > self._number_of_plates - 1:
     raise ValueError

continuation line over-indented for visual indent
Run Code Online (Sandbox Code Playgroud)

python

20
推荐指数
1
解决办法
2万
查看次数

打印之间的延迟

我正在尝试做的是每次打印时添加0.5秒的延迟.但我遇到的问题是它滞后了一段时间然后立刻打印出来.

import time

x = [(0, 1), (2, 3), (4, 5), (6, 7), (8, 7)]

for i in x:
    print(i)
    time.sleep(0.5)
Run Code Online (Sandbox Code Playgroud)

这应该做什么:

(0, 1)
wait 0.5 seconds
(2, 3)
wait 0.5 seconds
(4, 5)
etc
Run Code Online (Sandbox Code Playgroud)

然而,我的问题是它没有打印第一个并等待0.5并打印下一个,我的工作是等待这么久然后立即打印所有,我想知道什么是解决这个问题的方法.

python

2
推荐指数
1
解决办法
1908
查看次数

抛出RuntimeException,无法访问的行

public class ExceptionTest {

    public static void throwit(){
        throw new RuntimeException();
    }

    public static void main (String[]args){
        try{
            System.out.println("Hello");
            throwit(); // doesn't compile if this line is replaced with throw new RuntimeException.
            System.out.println("Done");
        } catch (RuntimeException e) {
            e.printStackTrace();
        } finally {
            System.out.println("Finally");
        }   
    }
}
Run Code Online (Sandbox Code Playgroud)

我正在尝试try-catch-finally并抛出异常,我无法弄清楚为什么我以某种方式得到错误,而另一种方式它编译得很好,即使他们做同样的事情.

我有一个叫做throwit()throw的新方法RuntimeException.我在main方法的try块下调用它,所有编译都很好并且工作正常.但是,如果我取代了throwit();throw new RuntimeException比低于该行不可达.

但是,是不是throwit()throw new RuntimeException做同样的事情?throwit()抛出一个新的RunetimeException,只是说throw new RuntimeException同样的事情.

所以,我的问题是为什么RuntimeException在直接调用时抛出一个没有错误的新编译的方法throw new RuntimeException会导致一条无法访问的行,即使它们做同样的事情呢?如果有人能为我回答这一点,我将非常感激.

java exception-handling

2
推荐指数
1
解决办法
384
查看次数

将数字反向添加到列表中

我试图将数字从num到1追加到第一个元组的列表中,但我无法弄清楚如何做到这一点.我想出了如何使用列表来完成它,但我不确定如何在没有列表的情况下执行此操作.我尝试从[i for i in revers(range(1,num + 1))]中删除[],但这不起作用.

到目前为止我所拥有的:

def put_to_list(num):
    items = ((0, []), (1, []), (2, []))
    items[0][1].append([i for i in reversed(range(1, num + 1))])
    return items[0]
Run Code Online (Sandbox Code Playgroud)

用上面的代码我得到:

put_to_list(10)
(0, [[10, 9, 8, 7, 6, 5, 4, 3, 2, 1]])
Run Code Online (Sandbox Code Playgroud)

有没有办法做我做的但不是括号,所以:

>>> put_to_list(10)
(0, [10, 9, 8, 7, 6, 5, 4, 3, 2, 1])
Run Code Online (Sandbox Code Playgroud)

python

1
推荐指数
1
解决办法
72
查看次数

如果将项目添加到2D阵列中不存在的位置,则可防止崩溃

我的代码:

public class MultiArr {

    private int row;
    private int col;
    private int MultiA[][];

    public MultiArr(int row, int col) {
        this.row = row;
        this.col = col;
        MultiA = new int[row][col];
    }

    public void setItem(int row, int col, int item) {
        if (row > MultiA.length || col > MultiA.length) {
            System.out.println("out of bound");
        } else {
            MultiA[row][col] = item;
        }
    }

    public int getItem(int row, int col) {
        return MultiA[row][col];
    }

    public int getLength() {
        return MultiA.length;
    }

    public static void …
Run Code Online (Sandbox Code Playgroud)

java arrays

1
推荐指数
1
解决办法
32
查看次数

构造对象实例时创建的输出是什么

假设以下代码运行时没有错误:

在此输入图像描述

并假设在其他一些类中我们有一个主方法: A var1 = new C();

我得到了这两个问题,我回答错了:

1)通过构造C的实例将创建什么输出?

我对此的回答是一些,我认为这是因为它说new C();,所以它创建了一个新的C对象,并在该类的构造函数中打印一些.但我错了,正确答案就是这里的一些.这对我来说没有意义,我真的不知道那是怎么回事.

2)如果使用方法调用,((B) var1).report()则调用哪个方法.

我对此的回答是B类中的方法报告.我认为这是正确的答案,因为var1是用B转换的,所以我假设它将使用B类中的方法.但我也错了.正确答案是C类中的方法.这对我来说也没有意义.

这非常令人困惑,我真的不知道如何得到这些答案.如果有人能够向我解释究竟发生了什么,以及他们如何得到那些真正值得赞赏的答案.

java

1
推荐指数
1
解决办法
44
查看次数

比较字符串中的字符

给定两个字符串作为参数返回true如果第一个字可以通过改变一个字母从第二个字形成.我接近这个的方式是:

def differ(word_one, word_two):
    '''(str, str) -> bool

    Returns true iff word_two can be formed from word_one by changing
    exactly one letter.

    >>> differ('cat', 'bat')
    True
    >>> differ('word', 'sword')
    False

    '''
    temp_list = []
    # If the length of the first string is equal to the length of the
    # second string, iterate over the letters in the first string and
    # if the letter in the first string does not equal to the letter 
    # in the second string …
Run Code Online (Sandbox Code Playgroud)

python string list

0
推荐指数
1
解决办法
76
查看次数

返回列表中的索引

def test(lst):        
        new_lst = []
        for i in lst:
                if i is True:
                        new_lst.append(lst.index(i))
        return new_lst
Run Code Online (Sandbox Code Playgroud)

上面的代码应该做什么:

循环遍历列表中的每个元素,如果元素为True,则将其附加到新列表中.然而,它并没有完全按照它应该做的那样做,我无法弄清楚我哪里出错了.

预期:

test([1,2,3,True,True,False])
[3,4]
Run Code Online (Sandbox Code Playgroud)

拿到:

[0,0]
Run Code Online (Sandbox Code Playgroud)

python list

0
推荐指数
1
解决办法
65
查看次数

标签 统计

python ×5

java ×3

list ×2

arrays ×1

exception-handling ×1

string ×1