简体   繁体   中英

How can I make the if function in python try the values from the range for “a” and return the correct answer

a = list(range(0,10))

if a + 6 == 10:
    print(a)

I am trying to make the numbers between 0 and 10 be individually substituted into a and when it finds the right answer then it will stop and return the value of a . So, in this case, just to return 4 .

I keep getting this error

can only concatenate list (not "int") to list

Here you go:

def func():
    for a in range(10):
        if a + 6 == 10:
            return a

You should iterate over the list a and then check your condition for each element of a

a = list(range(0,10))

for i in a:
    if i + 6 == 10:
        print(i)

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