#πŸ”’ Need help, not making progress

373 messages Β· Page 1 of 1 (latest)

median shoal
#

I've been making no progress for the past hours, the description of the task is in this post.

midnight thornBOT
#

@median shoal

Python help channel opened

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.

median shoal
#

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
thorny lagoon
median shoal
thorny lagoon
median shoal
#

not sure but I dont think that matters too much

#

I've managed to make a menu that prints this list

thorny lagoon
#

it might but anyways. What do you have so far in your code?

median shoal
#

I can share the code

thorny lagoon
#

!pastebin if its too long

midnight thornBOT
#
Pasting large amounts of code

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.

median shoal
#

here you go

#

something got messed up at line 48-49 with the copy and paste

thorny lagoon
#

So I'll assume you've done menu options 1 and 4. Can you do 2 and 3?

median shoal
#

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

thorny lagoon
#

designation (e.g. K)
name (e.g. Lysine)
group (e.g. alkaline)
molecular weight (e.g. 146.19)

median shoal
#

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

thorny lagoon
#

The user input for e.g. is CAAC

#

If you break that down, you have C, A, A, and C

median shoal
#

yeah

thorny lagoon
#

C x 2, A x 2

#

You take the values of each

#

What is C?

#

What is A?

median shoal
#

ofc ofc but how would you do that with programming language

thorny lagoon
#

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

median shoal
#
for row in listaAmino: # the list with all the aminoacids and its information
  print(row)
#

is this how youd get the weight?

thorny lagoon
#

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

median shoal
#

oh right right

#

but arent those locally available only?

thorny lagoon
#

yea

#

thats fine

#

since all of the data is already in the text file

#

so its "locally available"

median shoal
#

i dont quite understand how youd get the values then

#

but lets take it from the start

thorny lagoon
#

Do you understand your Aminoacid class object?

#

And how to use it?

median shoal
#

kind of

#

init stores all the properties right

thorny lagoon
#

right. Now how do you access one of those properties?

median shoal
#

you type your class and end it with a .

#

then you can what property you want to pick form

thorny lagoon
median shoal
#

amino1 = Aminoacid()

thorny lagoon
#

ok you almost got it

#

Aminoacid() on its own won't work because you've defined __init__ and it expects values in the ()

thorny lagoon
median shoal
#

dont know, is the name even connected to the weight at all?

thorny lagoon
#

it is

#

its in one class instance

median shoal
#

yeah but form the text file its just text in rows and columns right

#

and ive seperated the text file into rows

thorny lagoon
#

no. You already extracted from the text file and into a list of Aminoacid class instance. So you can forget the text file

median shoal
#

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(',')
thorny lagoon
#

kinda but also... hmm

median shoal
#

and by doing this I made each element in a row its own index

median shoal
#

okey good, then Im on track

thorny lagoon
#

Keep going

#

What happens from line 42 to 53?

median shoal
#

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

thorny lagoon
#

good so far

median shoal
thorny lagoon
#

It is a list per row

#

Not sure if your line 48 to 51 is a typo

#

Is that supposed to be there?

median shoal
#

thats a typo

#

from when I copied to the website

thorny lagoon
#

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

median shoal
#

not sure if we are allowed to use dictionary in this task

thorny lagoon
#

can you ask?

median shoal
#

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

thorny lagoon
#

alright then continue with list

median shoal
#

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()"

thorny lagoon
#

ok

median shoal
#

okey so for the list

#

I should create a list that can store all the sequences as in the example

#

and append every input

thorny lagoon
median shoal
#

right the listaAmino

thorny lagoon
#

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

median shoal
#

yeah, lets see if I can code the first step

#

also can I erase the second class Sequence?

thorny lagoon
#

I don't know but I don't think so

#

Because the assignment did say

3 – List all sequences sorter in order of weight

median shoal
#

okok

#

should you break down the string first?

#

e.g CAAC

thorny lagoon
#

why? its already iterable

median shoal
#

to see if each character matches with listaAmino

thorny lagoon
#

yea but you don't need to break it down

median shoal
#

you should use a for loop then

#

?

thorny lagoon
#

you access each character by their index

median shoal
#

mhm

#
for i in userinput:
thorny lagoon
#

!e

userInput = "CAAC"
print(userInput[0])
print(userInput[1])
midnight thornBOT
thorny lagoon
#

but yes, you use a for loop

#

and you loop over the length of the user input

median shoal
#
def matchInputWithAminoAcids():
    userInput = str(input())

    for i in range(len(userInput)):
thorny lagoon
#

you don't need to str() over the input(). It is by default a string

median shoal
#

but this doesnt tell me to check each character in userInput right?

thorny lagoon
median shoal
#

lets see

#
def matchInputWithAminoAcids():
    userInput = input()

    for i in range(len(userInput)):
        if userInput[0] in listaAmin:
thorny lagoon
#

almost

median shoal
#

I need to have listaAmin in matchInputWithAminoAcids():

thorny lagoon
#

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

#
  1. change your for loop so it loops over the userinput and use the variable from the for loop to check in the if condition
#
  1. change the if condition to use the running index from the loop
median shoal
#

for the second option

#

do you change 0 to i

thorny lagoon
#

yea

median shoal
#

alright nice

thorny lagoon
#

!e

fruit = "Apple"
for letter in fruit:
  print(letter)

If you're curious about option 1

midnight thornBOT
median shoal
#

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

thorny lagoon
#

you'd be surprised

#

Yes, you have to use another for loop

median shoal
thorny lagoon
#

Its why I suggested using a dictionary

median shoal
#

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

thorny lagoon
#

what does listaAmino show when you print it?

#

I assume a list of class objects, no?

median shoal
#

basically the text file

thorny lagoon
median shoal
#

one sec

thorny lagoon
#

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.

thorny lagoon
median shoal
#

thats from printing each row in listaAmin

#

or each aminoacid

median shoal
thorny lagoon
median shoal
#

havent been told to document my functions

thorny lagoon
median shoal
#

but if you ask for the radius for something, you can just assume that its a float or int

thorny lagoon
thorny lagoon
median shoal
#

yeah, imma implement it in the future

median shoal
thorny lagoon
#

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.

median shoal
thorny lagoon
median shoal
thorny lagoon
median shoal
#

test every aminoacid till userInput matches the name of the aminoacid

thorny lagoon
median shoal
#

is it another if statement?

thorny lagoon
#

you just need 1 if statement

median shoal
#

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

median shoal
#

it just goes on and does its own quest

#

will be back, I have another task that I have to present

median shoal
#

alright I'm back @thorny lagoon

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

median shoal
#

alright

#

lets see what I can do

#

I think I lost the progress

#

bruh

#

got it back, at least most of it

median shoal
thorny lagoon
median shoal
#
 for i in range(len(userInput)):
        if userInput[i] in listaAmino:
            for aminoacid in 
thorny lagoon
#

I was talking about the in in if statement

#

You obviously need the in for the for loop

median shoal
#
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

thorny lagoon
median shoal
#

it comes from no where really

#

but what eles should you write

thorny lagoon
#

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?

median shoal
thorny lagoon
#

Move it up

median shoal
#

alright

thorny lagoon
#

So it's for loop, for loop, then if

median shoal
#

got it

#

what do I want to do after

#

take the .weight of that aminoacid

thorny lagoon
#

Yes

median shoal
#

but I still dont know the accurate name of the aminoacid

thorny lagoon
#

Wdym?

median shoal
#

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

thorny lagoon
#

Right

#

So you still need to total the weights, right?

median shoal
#

yeah

#

do I append all the new weights to a new list

thorny lagoon
#

You don't have to

#

Just add them as you find them

#

totalweight = totalweight + aminoacid.weight

#

Don't forget to save the sequence

median shoal
#

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

thorny lagoon
#

I can see a problem. See if you can spot it

median shoal
#

aminoacid.moleculeWeight

#

this one?

thorny lagoon
#

That's one

median shoal
#

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

thorny lagoon
median shoal
#
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')
thorny lagoon
#

You can't access totalWeight outside of the function scope

#

unless you return it

thorny lagoon
#

!return-gif will explain it better

midnight thornBOT
#
Print and return

Here's a handy animation demonstrating how print and return differ in behavior.

See also: /tag return

median shoal
#
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)
thorny lagoon
#

The totalWeight error is another thing. You have not set a default value to totalWeight before the for loops

median shoal
#

okey in the function

#

I set it to 0

#

now it returns 0

#

does that mean aminoacid.weight didnt work

thorny lagoon
#

share the latest code

median shoal
#

sharing the whole code

thorny lagoon
median shoal
#

C only

thorny lagoon
#

That should work

#

print(aminoacid) before your if statement and try again

median shoal
#

the if statement in the menu?

thorny lagoon
median shoal
#

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
thorny lagoon
median shoal
thorny lagoon
median shoal
#

used CA instead C

#

but I get the same result if I type C only

thorny lagoon
#

ah wait. Forgot that you need to break the 2nd loop when you found a match

#

Also, change your print to show aminoacid.name

median shoal
#

change my print mhm

#

to what?

thorny lagoon
#

to aminoacid.name

#

Then test and show me the output of your test

median shoal
#

wait

#

where do you add the break

thorny lagoon
#

in your if

median shoal
#

so if else

#

and under else I add break

#

if i break directly after if

#

then totalWeight will be gone no?

thorny lagoon
#

so not directly after if

#

Where should you place it?

median shoal
#
    for i in range(len(userInput)):
        for aminoacid in listaAmino:
            aminoacid.name
            if userInput[i] == aminoacid.name:
                totalWeight = totalWeight + aminoacid.moleculeWeight
        break
thorny lagoon
#

no

median shoal
thorny lagoon
#

place the break in the if. but after the totalweight

thorny lagoon
median shoal
#

yeah

#

no print

thorny lagoon
#

are you gonna print it still or?

median shoal
#

I wouldnt print anything right now

#

except the totalWeight

thorny lagoon
#

ok

median shoal
#

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

thorny lagoon
#

no value?

#

Not 0?

median shoal
#

no, I have no print

#

wait

#

let me

#

print(something)

#

under choice 2

#

yeah now I got a 0

thorny lagoon
#

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?

median shoal
#

yeah we print it

#

and it worked for designation and name

#

let me try weight

thorny lagoon
#

oh wait...

#

is C a designation or name?

median shoal
#

designation

thorny lagoon
#

and your if statement is checking ... ?

median shoal
#

C for cystein and it checks for .name

#

bruh

thorny lagoon
#

ok so fix it up

median shoal
#
totalWeight = totalWeight + aminoacid.moleculeWeight
TypeError: unsupported operand type(s) for +: 'int' and 'str'
#

almost

#

which one is str

#

I set totalweight to = 0

thorny lagoon
median shoal
#

then we make it an int

#

can you go to class and int()

thorny lagoon
median shoal
#

yeah or float

thorny lagoon
#

yeah make them floats

median shoal
#

hel yeah

#

it works!

thorny lagoon
#

cool

median shoal
#

menu option 2 should be to save sequences

#

not to print the totalweight just yet

thorny lagoon
#

yup

median shoal
#

okey I just have to remove print(something)

thorny lagoon
#

you got this 🦾

median shoal
#

Thanks

midnight thornBOT
#
Python help channel closed

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.