#π Need help, not making progress
373 messages Β· Page 1 of 1 (latest)
@median shoal
Remember to:
- Ask your Python question, not if you can ask or if there's an expert who can help.
- Show a code sample as text (rather than a screenshot) and the error message, if you've got one.
- Explain what you expect to happen and what actually happens.
:warning: Do not pip install anything that isn't related to your question, especially if asked to over DMs.
Contents: Sorting, printing method (str).
In our cells, amino acids form a fundamental component where certain sequences of thousands of these form peptides. More specifically, there are three consecutive "steps" on the DNA spiral (actually RNA Links to an external site.) that code for an amino acid where each step can be one of the nitrogen bases CGAT Links to an external site. This forms a number system with the base 4 and with three "digits" (steps) we get 43 = 64 combinations, however, not all of these are used and we have 20 unique amino acids. Your task is to write a program that loads these from a text file (which is located under Files/Labs and is called "aminosyror.txt") and then stored as a list of objects where each object has the properties (= the instance variables):
designation (e.g. K)
name (e.g. Lysine)
group (e.g. alkaline)
molecular weight (e.g. 146.19)
You will also need a list of sequence objects where each such object has the properties:
sequence (e.g. ACCA)
Weight (e.g. 342.59)
A run might look like this (red text is the application's output and green is the user's input):
reading aminoacids...
...done.
Choose one of the following:
1 β List all amino acids
2 - Save a sequence of amino acids (peptide)
3 β List all sequences sorter in order of weight
4 - Exit
1
H Histidine Alkaline 155.16
K Lysine Alkaline 146.19
R Arginine Alkaline 174.2
C Cysteine hydrophilic 121.16
N Asparagine hydrophilic 132.12
Q Glutamine hydrophilic 146.15
S Serine hydrophilic 105.09
T threonine hydrophilic 119.12
Y Tyrosine hydrophilic 181.19
A Alanine hydrophobic 89.09
F Phenylalanine hydrophobic 165.19
G Glycine hydrophobic 75.07
I Isoleucine hydrophobic 131.17
L Leucine hydrophobic 131.17
M Methionine hydrophobic 149.21
P Proline hydrophobic 115.13
V Selected hydrophobic 117.15
W Tryptophan hydrophobic 204.23
D Aspartic Acid 133.1
E Glutamic Acid 147.13
Choose one of the following:
1 β List all amino acids
2 - Save a sequence of amino acids (peptide)
3 β List all sequences sorter in order of weight
4 - Exit
2
Specify a sequence: CAAC
Choose one of the following:
1 β List all amino acids
2 - Save a sequence of amino acids (peptide)
3 β List all sequences sorter in order of weight
4 - Exit
2
Specify a sequence: CA
Choose one of the following:
1 β List all amino acids
2 - Save a sequence of amino acids (peptide)
3 β List all sequences sorter in order of weight
4 - Exit
3
CAAC:420.5 APPROX:210.25
what does the content of the text file look like?
are those tabs after each , ?
not sure but I dont think that matters too much
I've managed to make a menu that prints this list
it might but anyways. What do you have so far in your code?
I can share the code
!pastebin if its too long
If your code is too long to fit in a codeblock in Discord, you can paste your code here:
https://paste.pythondiscord.com/
After pasting your code, save it by clicking the Paste! button in the bottom left, or by pressing CTRL + S. After doing that, you will be navigated to the new paste's page. Copy the URL and post it here so others can see it.
I hope you've fixed that indentation
So I'll assume you've done menu options 1 and 4. Can you do 2 and 3?
yeah. I've done 1 and 4 but cant do 2 and 3
if I input a sequence with input
it has no weight value from the textfile
and dont know why I would have my sequence as its own class
the weight value is the last column
designation (e.g. K)
name (e.g. Lysine)
group (e.g. alkaline)
molecular weight (e.g. 146.19)
right thats for the class Aminoacid
but menu option 2 asks for user input
and that input is only a string if im not mistaken
yeah
ofc ofc but how would you do that with programming language
Thats the point with the AminoAcid class. When you search up which class instance contain the designation, A or C, you get its weight value
for row in listaAmino: # the list with all the aminoacids and its information
print(row)
is this how youd get the weight?
no
You already got the weight in your for loop but you're not using the class to store them
And then later reference them in option 2 and 3
yea
thats fine
since all of the data is already in the text file
so its "locally available"
i dont quite understand how youd get the values then
but lets take it from the start
right. Now how do you access one of those properties?
you type your class and end it with a .
then you can what property you want to pick form
show me an example
ok you almost got it
Aminoacid() on its own won't work because you've defined __init__ and it expects values in the ()
So since you have the "name", how do you get its "weight"?
dont know, is the name even connected to the weight at all?
yeah but form the text file its just text in rows and columns right
and ive seperated the text file into rows
no. You already extracted from the text file and into a list of Aminoacid class instance. So you can forget the text file
okey so I opened the file, used readlines() and got it into a list
that is sorted in this way:
A Blah blah
B Blah blah
is this correct?
for row in data:
data = row.split(',')
kinda but also... hmm
and by doing this I made each element in a row its own index
thats right
okey good, then Im on track
then for each row, it now has indexes 0-3 (4 items which are designation, name, group and weight)
but im not sure if each row is a list now
anyway, each of these indexes are assigned to one of the properties for the class AminoAcid
and then each row is appended to a new list called listaAmino
good so far
what about this?
It is a list per row
Not sure if your line 48 to 51 is a typo
Is that supposed to be there?
alright so the next thing is you can either loop over your listaAmino or change it to a dictionary of aAmino and use the Amino Acid's name as the key
not sure if we are allowed to use dictionary in this task
can you ask?
the last task was also about classes and objects and we were supposed to use lits with objects only
its probably the same for this task aswell
alright then continue with list
there are some req that I just saw
Requirements for the solution:
You should use the main() method to eliminate the risk of using global variables locally.
If the "aminosyror.txt" file does not exist, an error message should be written (the program can then be terminated, but not crashed). To exit a Python program, you can import the module "sys" and then type "sys.exit()"
ok
okey so for the list
I should create a list that can store all the sequences as in the example
and append every input
No there's already a list created. Use the same list. And loop over it to find the matching name as per the user input
right the listaAmino
What you need to create is a total weight value (I assume that's what it is). So it totals to 420.5 for example
yeah, lets see if I can code the first step
also can I erase the second class Sequence?
I don't know but I don't think so
Because the assignment did say
3 β List all sequences sorter in order of weight
why? its already iterable
to see if each character matches with listaAmino
yea but you don't need to break it down
you access each character by their index
!e
userInput = "CAAC"
print(userInput[0])
print(userInput[1])
:white_check_mark: Your 3.12 eval job has completed with return code 0.
001 | C
002 | A
def matchInputWithAminoAcids():
userInput = str(input())
for i in range(len(userInput)):
you don't need to str() over the input(). It is by default a string
but this doesnt tell me to check each character in userInput right?
alright
It doesn't. You do that inside the for loop
lets see
def matchInputWithAminoAcids():
userInput = input()
for i in range(len(userInput)):
if userInput[0] in listaAmin:
almost
I need to have listaAmin in matchInputWithAminoAcids():
yea but your if statement is wrong
userInput[0] would mean that on each iteration of the loop, it will always check the first index of userInput
you have 2 ways to fix this
- change your for loop so it loops over the userinput and use the variable from the for loop to check in the if condition
- change the if condition to use the running index from the loop
yea
alright nice
!e
fruit = "Apple"
for letter in fruit:
print(letter)
If you're curious about option 1
:white_check_mark: Your 3.12 eval job has completed with return code 0.
001 | A
002 | p
003 | p
004 | l
005 | e
instead of letter, we have row which is one aminoacid
is the next step to use a for loop again?
no it doesnt sound right

Its why I suggested using a dictionary
yeah would be easier
def matchInputWithAminoAcids(listaAmino):
userInput = input()
for i in range(len(userInput)):
if userInput[i] in listaAmino:
for aminoacid in listAmino:
starting to get confusing
what does listaAmino show when you print it?
I assume a list of class objects, no?
basically the text file
Is it?
I'm also guessing (and hoping) that this assignment also wants you to document your functions, so we (especially for you) know what the function expects in the parameter.
This is from printing listaAmino?
write down what each function does?
What I mean by document, I also mean docstrings like
def yourfunction(x, y):
"""
@param x is an integer
@param y is an integer
@return an integer
"""
havent been told to document my functions
Right. Because you set up __str__ method. So its effectively a list of that class.
but if you ask for the radius for something, you can just assume that its a float or int
Its good practice to do. That's up to you if it doesn't say its required
Thats the point of a documentation. So it destroys ambiguity like that
yeah, imma implement it in the future
back to the code, is this correct?
Anyways, to check if the user input character in a list of class objects, may not work but you'll have to test it and find out.
dont know why im checking all the aminoacids in the for loop
How do you access the "name" property of an amino acid class object?
ok and in this context, how do you actually get it if its not myvariable?
test every aminoacid till userInput matches the name of the aminoacid
how do you do that?
you just need 1 if statement
alright
if userInput[i] in listaAmino:
this line only checks if its the list
good if it passes
then
okey if you have the user input CAAC
it takes the first index "C"
checks if its in the list, one row or aminoacid has a C in it
then it moves on to the second for loop
and then for every aminoacid in listaAmin
see, this step isnt connected to the "C" I just mentioned above
it just goes on and does its own quest
will be back, I have another task that I have to present
alright I'm back @thorny lagoon
OK. so don't use in
you really have to check on each AminoAcid class's name against the User Input's index
alright
lets see what I can do
I think I lost the progress
bruh
got it back, at least most of it
which of them, the first or the second
There's 2?
for i in range(len(userInput)):
if userInput[i] in listaAmino:
for aminoacid in
I was talking about the in in if statement
You obviously need the in for the for loop
def matchInputWithAminoAcids(listaAmino):
userInput = input()
for i in range(len(userInput)):
if userInput[i] == aminoacid.name:
for aminoacid in
if the character you get from the userinput is the same as aminoacid.name then go on
or should it be aminoacid.name in listaAmino
Think about it. Where does aminoacid come from in your if statement?
You can get it from your for loop
for whatevervariableyouwanthere in whateverlistyouhave
The whatevervariableyouwanthere is anything you want to refer to each element/item/index of that list
Like how I did for letter in fruit
That basically assigns the value of each index to the variable letter
And the value changes after each iteration/loop
Does that make sense?
but that for loop comes after the if statment?
Move it up
alright
So it's for loop, for loop, then if
Yes
but I still dont know the accurate name of the aminoacid
Wdym?
but python does
if the == statement goes through
it means that a match has been made
nvm I can just look at the input and see what character passed if statement
You don't have to
Just add them as you find them
totalweight = totalweight + aminoacid.weight
Don't forget to save the sequence
and for that I need to save it into a list
def matchInputWithAminoAcids(listaAmino):
userInput = input()
for i in range(len(userInput)):
for aminoacid in listaAmino:
if userInput[i] == aminoacid.name:
totalWeight = totalWeight + aminoacid.moleculeWeight
return totalWeight
so far it looks like this
I can see a problem. See if you can spot it
That's one
nice
maybe its better to append totalweight to a list directly
or why would I do that
maybe I should use the class Sequence
because now I got the sequence (order) and the weight
a dictionary would be good rn
also it seems like I cant print totalWeight
And why is that?
UnboundLocalError: cannot access local variable 'totalWeight' where it is not associated with a value
thats when I used print in elif choice 2
while True:
# menu
print('1 -')
print('2 -')
print('3 -')
print('4 -')
choice = int(input())
if choice == 1:
showListOfAminoacids(listaAmino)
elif choice == 2:
matchInputWithAminoAcids(listaAmino)
print(totalWeight)
elif choice == 3:
pass
elif choice == 4:
sys.exit()
else:
print('\nGive a correct alternative\n')
I do return it here
!return-gif will explain it better
something = matchInputWithAminoAcids(listaAmino)
tried that
but then it ruins the menu
and it asks for userinput directly
or do I have to put totalweight here
matchInputWithAminoAcids(listaAmino, totalWeight)
this is right. Then you print something
The totalWeight error is another thing. You have not set a default value to totalWeight before the for loops
okey in the function
I set it to 0
now it returns 0
does that mean aminoacid.weight didnt work
share the latest code
What did you put as your input?
C only
the if statement in the menu?
no. In the matchInputWithAminoAcids
doesnt work
it prints the text file basically
and returns 0
def matchInputWithAminoAcids(listaAmino):
totalWeight = 0
userInput = input()
for i in range(len(userInput)):
for aminoacid in listaAmino:
print(aminoacid)
if userInput[i] == aminoacid.name:
totalWeight = totalWeight + aminoacid.moleculeWeight
return totalWeight
can you share a screenshot of what you see?
can I see before that?
ah wait. Forgot that you need to break the 2nd loop when you found a match
Also, change your print to show aminoacid.name
in your if
so if else
and under else I add break
if i break directly after if
then totalWeight will be gone no?
you got it
so not directly after if
Where should you place it?
for i in range(len(userInput)):
for aminoacid in listaAmino:
aminoacid.name
if userInput[i] == aminoacid.name:
totalWeight = totalWeight + aminoacid.moleculeWeight
break
no

place the break in the if. but after the totalweight
This aminoacid.name is not inside print()
are you gonna print it still or?
ok
yeah the list works if you want to print something
but now I get no value returned from totalweight
which is good because I shouldnt
no, I have no print
wait
let me
print(something)
under choice 2
yeah now I got a 0
Thats still there, right?
ok
So the problem we're trying to debug is, if aminoacid does contain properties like name or moleculeWeight.
Do you know how to find out?
designation
and your if statement is checking ... ?
ok so fix it up
totalWeight = totalWeight + aminoacid.moleculeWeight
TypeError: unsupported operand type(s) for +: 'int' and 'str'
almost
which one is str
I set totalweight to = 0
The values for moleculeWeight is a string
Best if you make it an int when you make them into the class instance
yeah or float
yeah make them floats
cool
yup
okey I just have to remove print(something)
you got this π¦Ύ
Thanks
This help channel has been closed and it's no longer possible to send messages here. If your question wasn't answered, feel free to create a new post in #1035199133436354600. To maximize your chances of getting a response, check out this guide on asking good questions.