小编ins*_*get的帖子

Python:我的嵌套循环有什么问题?

使用while循环,我写了需要输入一个正整数,并打印出显示所有的整数乘法直至并包括输入号码的乘法表的过程.方程式的打印顺序很重要.

例如,print_multiplication_table(2)给出:
1*1
1*2
2*1
2*2

这是我的代码:

def print_multiplication_table(n):
    a = 1
    b = 1
    while a <= n:
        while b <= n:
            print str(a) + " * " + str(b)
            b = b + 1
        a = a + 1
Run Code Online (Sandbox Code Playgroud)

但是,这似乎不起作用,因为它只打印出来

1 * 1
1 * 2
Run Code Online (Sandbox Code Playgroud)

有谁知道为什么?谢谢!

python loops nested while-loop

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

将单个字符转换为小写

说明:第一步是编写一个lowerChar(char)函数,该函数可以返回将单个字符char转换为小写字母的结果。它应该执行以下操作:

如果输入字符char是大写字母(在“ A”和“ Z”之间),则应返回字母的小写版本(在“ a”和“ z”之间)。在所有其他情况下,它应返回与输入相同的字符。

(为了执行第一步,您将必须使用if语句,an和运算符,并应用本课中有关字符串的一些知识。)

我所拥有的是-

def lowerChar(char):
Run Code Online (Sandbox Code Playgroud)

python

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

如何将列数据解析为数组?

我有一个文件如下:

1 99
2 33
3 90
4 25
5 89
Run Code Online (Sandbox Code Playgroud)

我想解析1,2,3,4,5列表x99, 33, 90, 25, 89列入列表y,我该怎么做?

我不将部分"明白的是,我可以的ReadLine但不知道如何解析到两个列表,我想也许有一个更优雅的方式来给它除非空间劈裂,并添加words[0]xword[1]y

python

-3
推荐指数
1
解决办法
80
查看次数

程序继续直接使用else语句(Python)

number1 = float
number2 = float


number1 = raw_input("Please input the first number: ")
number2 = raw_input("Please input the second number: ")


if number1 > number2:
    print number1 + ' is bigger than ' + number2

elif number2 < number1:
    print number2 + ' is bigger than ' + number1

else:
    print 'You did not follow the instructions properly. Goodbye!'


print "\n"
raw_input("Please press enter to exit.")
Run Code Online (Sandbox Code Playgroud)

python if-statement

-3
推荐指数
1
解决办法
257
查看次数

虚函数

什么是虚拟功能?什么是纯虚函数?请举例说明

c++ java

-4
推荐指数
1
解决办法
714
查看次数

抑制输出MATLAB

我的代码输出给出了if,exact_answer,然后是带有N个条目的向量输出.我不确定如何压制这个条目.例如,它看起来如下所示:

exact_answer =

    0.2642


If =

    0.1882


ans =

         0    0.1637    0.2681    0.3293    0.3595    0.3679
Run Code Online (Sandbox Code Playgroud)

我不想要答案输出. -

function g = LaplaceTransform(s,N)
        % define function parameters
        a=0; 
        b=1;
        h=(b-a)/N;
        x = 0:h:1;
        % define function
        g = ff(x).*exp(-s*x);

% compute the exact answer of the integral
exact_answer=antiderivative(b,s)-antiderivative(a,s)
% compute the composite trapezoid sum
If=0;
for i=1:(N-1)
    If=If+g(i).*h;
end;
If=If+g(1).*h/2+g(N).*h/2;
If
Run Code Online (Sandbox Code Playgroud)

matlab

-9
推荐指数
1
解决办法
718
查看次数

将getattr与Dictionary结合使用

class idDict(dict):
    def __getattr__(self,key):
        return 'dsaads'
d=idDict()
print d['ss']
Run Code Online (Sandbox Code Playgroud)

我刚收到一个错误:KeyError: 'ss'.根据一些在线论坛,这应该工作.我想打印'dsaads'

这也不起作用; 我犯了同样的错误:

from collections import defaultdict
class idDict(defaultdict):
    def __getattr__(self,key):
        return 'dsaads'
d=idDict()
print d['ss']
Run Code Online (Sandbox Code Playgroud)

python dictionary

-9
推荐指数
1
解决办法
1594
查看次数

标签 统计

python ×5

c++ ×1

dictionary ×1

if-statement ×1

java ×1

loops ×1

matlab ×1

nested ×1

while-loop ×1