Error name 'A' not defined - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: General Coding Help (https://python-forum.io/forum-8.html) +--- Thread: Error name 'A' not defined (/thread-21295.html) |
Error name 'A' not defined - RavCOder - Sep-23-2019 Hi, I have this error in my code Traceback (most recent call last) : File main.py,line 10 , in <module> nucleotide = [A,C,G,T] Error name 'A' not defined nucleotide = [A,C,G,T] nucleotide_input= input() if nucleotide_input != nucleotide: print("That's not a nucleotide...Try again") else: print(nucleotide_input)I don't know how this error don't compile my code , I think that I declared my variable correctly. Regards, RavCoder RE: Error name 'A' not defined - ichabod801 - Sep-23-2019 Where did you define A? Not in the code you're showing. Did you perhaps mean to put the string 'A' instead? RE: Error name 'A' not defined - RavCOder - Sep-23-2019 I wanted to put it like this in such a way that then it makes me the condition that if there was not one of those letters it would print the message to me otherwise I will print the nucleotide. I will try to put into a string , but I don't know how to do verify if user have inserted the correct value o no . nucleotide = "ACGT" RE: Error name 'A' not defined - ichabod801 - Sep-23-2019 Okay, then I would do it this way: nucleotide = set('ACGT') nucleotide_input= input() if nucleotide_input not in nucleotide: print("That's not a nucleotide...Try again") else: print(nucleotide_input)I use the in operator in the if condition. That checks if any of the items in the sequence on the right are equal to the item on the left. I use a set because sets are vary fast at checking the 'in' operator. Here's a subtle twist: if you give set an iterable, it takes each item in the iterable and puts it into the set. A string is an iterable that has characters as items, so each character is put separately into the set. Say you had done this: if nucleotide_input not in 'ACGT':That would have correctly said 'A', 'C', 'G', and 'T' were nucleotides. But it also would have said 'AC' or 'CGT' was a nucleotide, because the in operator for strings checks for substrings being in the string on the right. By using a set of the individual characters, we only allow 'A', 'C', 'G', and 'T' to be recognized as nucleotides. |