Is my is_valid function wrong?


#1

HI! This is not an exercise on codecademy, but rather my self-practice. Anybody can tell where I did wrong? I want the result to print out True or False every time I type in a character
while 1==1:
base=raw_input()
List=["A","C","G","T"]
def is_valid(base):
if base==List[0] or List[1] or List[2] or List[3]:
return True
print True
else:
return False
print False


#2

Hi,

let's try to understand what your code actually does, to see the errors :smiley:

You should know that you are entering an infinite-loop, which you will never break out from :confused:
You could put a break at the end of the line, but instead, you should just delete this line, since it won't help much :slightly_smiling:

In the next line you want the user to type in a string and define a list with capital letters, all good! :smiley:

You define a function with a variable called base. If the typed character base is the same as one of the letters in the list, it should return True and also print True. If that's not the case, then return False instead and also print False.

You should know that the function will stop after any return, so the print True should be in the line over return and the same for print False.

At the end you should call the function with:

is_valid(base)

to actually call the function with the written letter in the raw_input above.


#3

But I want to have the program kept letting user input character unless the user input includes nothing


#4

Then try this:

go = True

while go:
    base=raw_input()
    if base != "":
        go = False

#5

while True directly will be fine. Thanks!


#6

Sorry my bad, may misunderstood you


#7

go=True
while go:
base=raw_input()
def is_valid(base):
if base==List[0] or List[1] or List[2] or List[3]:
return True
else:
return False
print is_valid(base)

the result:
Enter your sequence one base at a time.
A
True
D
True
f
True

why it keeps printing out True even if I typed a wrong input intentionally?


#8

There is some issues with your if statement. To make it actually easier you can only check, if base is in the list:

if base in List:

So the whole code should look like this:

go=True
List=["A","C","G","T"]
while go:
    base=raw_input()
    if base != "":
        go = False
def is_valid(base):
    if base in List:
        return True
    else:
        return False
print is_valid(base)

#9

don't we need an indentation before the function?
otherwise the function will be actually outside the loop and while loop will be no use. Currently the program only allow me to input once


#10

No, you should never put a function into a loop. Think of the function as some kind of blueprint, which you can use every time you need them. Just call the function and it works good enough :smiley: . If you want to run the program for a few times, use a for-loop instead to set, how many times you want to run it.

If you want to use the function over and over, define it at the start of the program, so you can use it before.

def is_valid(base):
    if base in List:
        return True
    else:
        return False

List=["A","C","G","T"]

for x in range(3) # Run this code for 3 times
    base=raw_input()
    if base != "":
        print is_valid(base)

#11

Thanks very much. It works!


#12

You're welcome, feel free to ask me anytime you need help.

And also happy to hear that... or read :smirk:


#13

m = [1, 2, 3]
n = [4, 5, 6]

Add your code here!

def join_lists(x,y):
result=0
for i in range(len(x)):
result=result+x[i]
for j in range(len(y)):
result=result+y[i]
result=x[i]+y[i]
return result
print join_lists(m, n)

You want this to print [1, 2, 3, 4, 5, 6]

How about this problem? "Using two lists as two arguments in a function"?


#14

First of all, could you please mark your whole code and click on </>, so I can see the indentations better :smiley:

Now let's get back to your problem. You want to add the two lists and create one new list with both lists in it, right?

for i in range(len(x)):

Just type:

for i in x (shorter way and does the same :slightly_smiling: )

Now lets analyze your code:

You go through each list element and add it to results, but don't actually add it to a list.

Use instead result = [], so you can append the list with each element

def join_lists(x,y):
    result=[]
    for i in x:
        result.append(i)
    for j in y:
        result.append(j)
    return result
print join_lists(m, n)

This should work fine :smiley: