简体   繁体   English

为什么我的程序不能将整数列表转换为文本(字符串)?

[英]why can't my program convert a list of integars to text (string)?

I want to convert a list of integars (after picking up the prime numbers) to text,one str (not a list of strings).我想将整数列表(在提取素数之后)转换为文本,一个 str(不是字符串列表)。 How it can be acheived in a effective way as when i run the program it displays nothing not even 'error' Btw the program is supposed to display: "11-19"如何以有效的方式实现它,因为当我运行程序时它什么也没有显示,甚至没有“错误”顺便说一句程序应该显示:“11-19”

this is my code:这是我的代码:

v=[11,18,19]
n=3
def premier(v):   
        I=2
        valid=True
        while valid==True and  ( I<=(v/2) ) :
            if (v%I !=0) :
                I +=1              
            else:
                valide=False                
        return(valid)
def chaine(v,n):
    ch=""
    for i in range(n):
        if premier(v[i]):
            ch=ch+str(v[i])+"-"
    ch=ch[:len(ch)-1]
    return(ch)
print(chaine(v,n))

You have a typo in the first else statement of the function premier , which causes an infinite loop.您在premier prime 的第一个else语句中有错字,这会导致无限循环。

v=[11,18,19]
n=3
def premier(v):
        I=2
        valid=True
        while valid==True and  ( I<=(v/2) ) :
            if (v%I !=0) :
                I +=1
            else:
                valid=False
        return(valid)

def chaine(v,n):
    ch=""
    for i in range(n):
        if premier(v[i]):
            ch=ch+str(v[i])+"-"
    ch=ch[:len(ch)-1]
    return(ch)
print(chaine(v,n))

With this minor change, your code should work fine.通过这个微小的更改,您的代码应该可以正常工作。 Let me know if this works for you.让我知道这是否适合您。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM