小编iKy*_*aki的帖子

在Python中查找数字的倍数

我正在尝试编写一个代码,让我找到一个数字的前几个倍数.这是我的一次尝试:

def printMultiples(n, m):
for m in (n,m):
    print(n, end = ' ')
Run Code Online (Sandbox Code Playgroud)

我发现,通过推杆for m in (n, m):,无论数量是多少,它都会在循环中运行m.

def printMultiples(n, m):
'takes n and m as integers and finds all first m multiples of n'
for m in (n,m):
    if n % 2 == 0:
        while n < 0:
            print(n)
Run Code Online (Sandbox Code Playgroud)

经过多次搜索,我只能在java中找到一个示例代码,所以我尝试将其转换为python,但我没有得到任何结果.我有一种感觉我应该range()在这个地方使用这个功能,但我不知道在哪里.

python math range

9
推荐指数
1
解决办法
7万
查看次数

查找列表中的每个第n个元素

我试图找到列表中的每个第n个元素,但我很遗憾.这是我的代码:

__PRE__

我想[a [:: n]]可以用来查找结果,但我只是得到了int不可订阅的错误消息.对于列表[1,2,3,4,5,6],returnNth(1,2)应返回[1,3,5]和列表["dog","cat",3,"hamster" ,True],returnNth(你好,2)应该返回['dog',3,True]我可以用什么来解决这个问题?

python

6
推荐指数
1
解决办法
3万
查看次数

试图打印具有一定长度的单词数

此函数的格式为numLen(s,n):其中s是字符串,n是整数.代码应该做的是返回字符串中长度为n的单词数,所以:

numLen("这是一个测试",4)

将返回2,因为两个单词有4个字符.

def numLen(s, n):
'''
takes string s and integer n as parameters and returns the number of words
in the string s that have length n
'''
return s.split()
if len(s) == n:
    return 'hello'
Run Code Online (Sandbox Code Playgroud)

我试图将字符串拆分成一个列表并检查该列表中每个单词的长度,但这似乎没有成功.我设法得到的最远的是当我用14替换4时返回"你好",只是为了查看长度代码是否有效.

python

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

编写程序打印"Hello,world!" 程序

我刚刚开始阅读Accelerated C++,当我遇到这个时,我正在努力完成练习:

0-4. Write a program that, when run, writes the Hello, world! program as its output.

所以我提出了这个代码:

#include "stdafx.h"
#include <iostream>

using namespace std;

int main()
{
    cout << helloWorld << endl;

    cin.get();
    return 0;
}

void helloWorld(void)
{
    cout << "Hello, world!" << endl;
}
Run Code Online (Sandbox Code Playgroud)

我一直在收到错误'helloWorld' : undeclared identifier.我想我应该做的是为helloWorld创建一个函数,然后为输出调用该函数,但显然这不是我需要的.我也试过投入helloWorld()主力,但这也没有帮助.任何帮助是极大的赞赏.

c++ metaprogramming

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

根据前面的字符串返回子字符串

今天可能有点太多了......但是,好吧.

这个问题让我很困惑.此函数将字符串列表作为参数,并返回每个字符串,该字符串是其前面的字符串的子字符串.所以

  1. ["希望","跳","希望","测试","测试"]将返回['hop']
  2. ["希望","希望","跳","测试","测试"]将返回['hope','hop','test']

请原谅我这里乱七八糟的代码,我还在学习.

def findSubStrs(lst):
'list ==> list, return list of all strings that are substrings of their predecessor in lst'
res = []
for a in lst:
    if len(int(a-1)) > len(lst):
        res = res + [a]
return res
Run Code Online (Sandbox Code Playgroud)

我认为len(int(a-1))可以检查前面的字符串,但我得到错误消息"TypeError:不支持的操作数类型 - :'str'和'int'"唯一的结果我发现有效的是len(a)<3或其他一些int,但这并没有返回我需要的一切.

python substring

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

使用字典翻译短语

我试图通过将该短语中的每个单词与字典的键匹配来使用字典翻译短语.

http://paste.org/62195

我可以通过交互式shell很好地翻译它,但是当涉及到实际的代码时:

def translate(dict):
    'dict ==> string, provides a translation of a typed phrase'
    string = 'Hello'
    phrase = input('Enter a phrase: ')
    if string in phrase:
         if string in dict:
            answer = phrase.replace(string, dict[string])
            return answer
Run Code Online (Sandbox Code Playgroud)

我不确定将字符串设置为什么来检查"Hello"以外的任何内容.

python

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

将元音乘以字符串

我正在尝试将一个字符串作为输入并返回相同的字符串,每个元音乘以4和"!" 最后添加.防爆.'你好'回归,'heeeelloooo!'

def exclamation(s):
'string ==> string, returns the string with every vowel repeating four times and an exclamation mark at the end'
vowels = 'aeiouAEIOU'
res = ''
for a in s:
    if a in vowels:
        return s.replace(a, a * 4) + '!'
Run Code Online (Sandbox Code Playgroud)

上面的代码只返回'heeeello!' 我也尝试在交互式shell中使用元音等于('a','e','i','o','u'),但使用相同的代码导致:

>>> for a in s:
if a in vowels:
    s.replace(a, a * 4) + '!'
Run Code Online (Sandbox Code Playgroud)

'heeeello!' 'helloooo!'

我怎样才能使它成倍增加每个元音而不仅仅是其中一个元音?

python

0
推荐指数
2
解决办法
774
查看次数

标签 统计

python ×6

c++ ×1

math ×1

metaprogramming ×1

range ×1

substring ×1