python字符串总字数_计算Python字符串的唯一字符总数

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.

解决方案Your function get_user_input() has no return value

get_user_input.count('A') contains a function call, it will not work without parenthesis. Correct: get_user_input().count('A')

As mentioned in the comments, get_user_input() should only be called once since the users input will be collected at each call via the input() function.

This should work:

def count_bases():

char_list = get_user_input()

for char in 'ATCG':

char_count = char_list.count(char)

if char_count < 1:

print(char + " not found")

else:

print(char + " count: " + str(char_count))

def get_user_input():

one = input("Please enter DNA bases: ")

two=list(one)

return two

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值