简体   繁体   中英

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). 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"

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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