Counting total number of unique characters for Python string

2024/10/9 20:21:18

enter image description here

For my question above, I'm terribly stuck. So far, the code I have come up with is:

def count_bases():get_user_input()amountA=get_user_input.count('A')if amountA == 0:print("wrong")else:print ("right",amountA)def get_user_input():one = input("Please enter DNA bases: ")two=list(one)print(two)

My line of thinking is that I first:
1. Ask user to enter the DNA bases (ATCG)
2. Change the user input into a list
3. Going back to the main (count_bases) function, I count the number of 'A', 'T', 'C', 'G'
4. Use 4 if-else statements for the four different bases.

So far, my code only works up to the output of the user's input into a list. After that, an error just pops up.
Appreciate it if someone can point the right path out to me!
Thanks.

Answer

You can use collections.Counter

from collections import Counterdef get_user_input():input_str = input("Please enter DNA bases: ")c = dict(Counter('ACCAGGA'))return cdef count_bases():counts = get_user_input()dna_base = list("ATCG")for base in dna_base:if base not in counts.keys():print(f"{base} not found")else:print(f"{base} count: {counts[base]}")

output when I call count_bases()

Please enter DNA bases: >? ACCAGGCA
A count: 3
T not found
C count: 2
G count: 2
https://en.xdnf.cn/q/118538.html

Related Q&A

adding a newly created and uploaded package to pycharm

I created a package (thompcoUtils) on test.pypi.org and pypi.org https://pypi.org/project/thompcoUtils/ and https://test.pypi.org/project/thompcoUtils/ show the package is installed in both the test an…

Using builtin name as local variable but also as builtin [duplicate]

This question already has answers here:UnboundLocalError trying to use a variable (supposed to be global) that is (re)assigned (even after first use)(14 answers)Closed 1 year ago.I have the following f…

How to print the results of a SQLite query in python?

Im trying to print the results of this SQLite query to check whether it has stored the data within the database. At the moment it just prints None. Is there a way to open the database in a program like…

python sort strings with leading numbers alphabetically

I have a list of filenames, each of them beginning with a leading number:10_file 11_file 1_file 20_file 21_file 2_file ...I need to put it in this order:1_file 10_file 11_file 2_file 21_file 22_file ..…

Javascript is not recognizing a Flask variable

Im passing a set of variables into a Flask template, and I would like to first manipulate them with Javascript. The problem is that when I use the {{ var }} syntax, Javascript isnt recognizing it. The …

Float sum broken? [duplicate]

This question already has answers here:Is floating-point math broken?(36 answers)Closed 9 years ago.print(0.1 + 0.2 == 0.3)returnsFalseWhy?

SyntaxError: EOL while scanning string literal -Python [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.This question was caused by a typo or a problem that can no longer be reproduced. While similar q…

Formatting a return value from a serial device

I am reading a value from a device over serial, and the return value has the format: [Theoretical position in mm, Encoder position in mm], for example, b\r#-0.001504,-0.001516\n I would like to format …

if Else statement inside for loop is not working [duplicate]

This question already has answers here:Im getting an IndentationError (or a TabError). How do I fix it?(6 answers)Closed 6 months ago.am using following code to check certain conditionsfor myarg in my…

Sending data back and forth from android server to python client

I have posted this few days back but now i ran into another problem after solving that one. DESCRIPTION: working on an android app written in kotlin that behaves as a server side and Python program tha…