Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Error name 'A' not defined
#1
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
Reply
#2
Where did you define A? Not in the code you're showing. Did you perhaps mean to put the string 'A' instead?
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#3
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"
Reply
#4
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.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Variable is not defined error when trying to use my custom function code fnafgamer239 4 570 Nov-23-2023, 02:53 PM
Last Post: rob101
  [variable] is not defined error arises despite variable being defined TheTypicalDoge 4 2,119 Apr-05-2022, 04:55 AM
Last Post: deanhystad
  Error 'Contour' not Defined DaveG 3 2,331 Mar-13-2022, 03:29 AM
Last Post: deanhystad
  Getting "name 'get_weather' is not defined error and no json_data returned? trthskr4 6 3,588 Sep-14-2021, 09:55 AM
Last Post: trthskr4
  Error when refering to class defined in 'main' in an imported module HeRo 2 2,370 Apr-13-2021, 07:22 PM
Last Post: HeRo
  Why does lambda throw 'name value_o is not defined' error? karabakh 3 2,167 Dec-14-2020, 05:45 PM
Last Post: karabakh
  name error "name"is not defined MaartenRo 1 3,417 Jul-28-2020, 02:39 AM
Last Post: bowlofred
  Name Error: name 'Stockton' is not defined Pinokchu 3 2,267 Jun-13-2020, 02:48 PM
Last Post: Yoriz
  python library not defined in user defined function johnEmScott 2 3,824 May-30-2020, 04:14 AM
Last Post: DT2000
  error ,,name append is not defined'' Killdoz 1 5,019 May-24-2020, 06:23 PM
Last Post: bowlofred

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020