小编fif*_*man的帖子

列表中最长的字符串

我正在创建一个从列表中返回最长字符串值的函数.当只有一个字符串最多的字符串时,我的代码才有效.我试图让它打印所有最长的字符串,如果有多个字符串,我不希望它们被重复.当我运行它时,它只返回'hello',而我希望它还返回'ohman'和'yoloo'.我觉得这个问题在线if item not list:,但我已经尝试了一切,但它不起作用.

list = ['hi', 'hello', 'hey','ohman', 'yoloo', 'hello']
def length(lists):
    a = 0 
    answer = ''
    for item in lists:
        x = len(item) 
    if x > a:
        a = x
        answer = item
    elif x == a:
        if item not in list:
            answer = answer + ' ' + item
    return answer
print length(list)
Run Code Online (Sandbox Code Playgroud)

python

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

C++ while循环不起作用

#include <iostream>
#include <string>

using namespace std;

int main()
{
    string option;
    cout << "Would like water, beer, or rum?--> ";
    cin >> option;
    while( option != "water" || option != "beer" || option != "rum" )
        {
        cout << "You did not choose a valid option. Try again.\n";
        cout << "Would you like water, beer, or rum?-->";
        cin >> option;
        }
}
Run Code Online (Sandbox Code Playgroud)

为什么即使用户输入正确的选项,此代码也不会退出循环?

c++

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

添加while循环时C++出现问题

#include <iostream>
#include <string>

using namespace std;

int main()
{
    int i = 0;
    while ( i < 11)
        cout << i << '\n';
        i++;
}
Run Code Online (Sandbox Code Playgroud)

为什么这段代码重复输出0而不是每次都加1?

c++

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

简单的python代码

在编程中,我正在编写一个用户选择实体的代码,然后计算体积和表面积.但是我没有直接选择,而是向用户提供了询问有关实体的额外信息的选项,如果他们输入数字然后"显示"(例如'1 show').他们可以继续询问,直到选择一个实体,所以我使用了一个while循环.

循环不起作用.如果循环的条件无效,它仍会进入循环并且不让我离开.请帮忙.我觉得这很简单.我试图进行整数和字符串转换,没有它帮助.

#11/07/12 
#A program that asks user for input and calculates SA and Volume of a chosen solid

print "Which solid would you like to calculate?"    
print "  "      
print "1. Tetrahedron"          
print "2. Dodecahedron"           
print "3. Cone"                 
print "4. Sphere"                        
print "5. Icosahedron"                    
print '  '                        
print "If you want extra information on the solid, enter the solid's number and then add show. Example: 1 show"                   
print "If not, just enter the number"                       
choice = raw_input('----->')                             

#Where …
Run Code Online (Sandbox Code Playgroud)

python

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

简单的Python列表连接

我有一个字符串列表,每个字符串作为一个字符.他们按照这个词的顺序排列.如何将每个字符放在一个字符串中.例:

list_characters = ['H', 'e', 'l', 'l', 'o']
Run Code Online (Sandbox Code Playgroud)

成为:

'Hello'
Run Code Online (Sandbox Code Playgroud)

请帮助,谢谢

python

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

为什么这个程序在输出一些元素后会崩溃

#include <iostream>
#include <ctime>
#include <cstdlib>
using namespace std;

int main()
{
    srand(time(NULL));
    int sizer = (rand() % 15) + 2;
    int nenad[sizer];
    for (int i = 0; i < sizer;i++)
        nenad[i] = (rand() % 15) + 1;
    for (int i = 0; i < sizer;i++)
        cout << nenad[i] << endl;
    for (int i = 0; i < sizer;i++)
    {
        for (int j = i + 1;i < sizer;j++)
        {
            if (nenad[i] > nenad[j])
            {
                int temp = nenad[i];
                nenad[i] …
Run Code Online (Sandbox Code Playgroud)

c++

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

标签 统计

c++ ×3

python ×3