我試圖計算Rosalind問題的DNA序列的GC含量(%)。我有以下代碼,但它返回0,或者僅返回G的數量或C的數量(無百分比)。用於計算DNA序列中GC含量的初學Python腳本
x = raw_input("Sequence?:").upper()
total = len(x)
c = x.count("C")
g = x.count("G")
gc_total = g+c
gc_content = gc_total/total
print gc_content
我也試過這個,只是爲了讓G公司和C公司,而不是百分比的計數,但它只是返回整個字符串的計數:
x = raw_input("Sequence?:").upper()
def gc(n):
count = 0
for i in n:
if i == "C" or "G":
count = count + 1
else:
count = count
return count
gc(x)
編輯:我定了在第一個代碼示例的print語句中輸入錯字。這不是問題,我只是粘貼了錯誤的代碼片段(有很多嘗試...)
第一個可能是一個錯字,但你說的不是 'gc_content' cg_content「。第二個例子中沒有必要使用else語句。 – squiguy
我修改了它。這不是問題的根源,我只是從我嘗試不同事情的許多嘗試中粘貼了錯誤的代碼塊。 – jstewartmitchel