[英]How to ask user's input to make a code interactive
I created this code: 我创建了以下代码:
gene = open("AY365046.1.txt","r")
g=0;
a=0;
c=0;
t=0;
gene.readline()
for line in gene:
line = line.lower()
for char in line:
if char == "g":
g+=1
if char == "a":
a+=1
if char == "c":
c+=1
if char == "t":
t+=1
print "Guanina: " + str(g)
print "Adenina: " + str(a)
print "Citosina: " + str(c)
print "Timina: " + str(t)
gc = (g+c+0.) / (a+t+c+g+0.)
print "Conteúdo GC: " +str(gc)
Now I want to make it interactive... My objective is use the input() funcion to get the "sequence number" which will display the corresponding data... 现在我要使其具有交互性...我的目标是使用input()函数获取“序列号”,该序列号将显示相应的数据...
On the code above, it obtains only the data of one sequence/file (AY365046.1.txt)... Therefore, I need the code to get access to more files (for exemple, sequence1.txt and sequence2.txt )... And then, get the data of g , a , c and t on the sequence/file informed on the input() function... 在上面的代码中,它仅获取一个序列/文件(AY365046.1.txt)的数据...因此,我需要该代码才能访问更多文件(例如, sequence1.txt和sequence2.txt )。 ..然后,获取由input()函数通知的序列/文件中g , a , c和t的数据...
For exemple: 举个例子:
1) The system ask for the Sequence Number 1)系统要求序列号
2) The user type sequence2 2)用户类型序列2
3) The system get data from sequence2.txt 3)系统从sequence2.txt获取数据
4) The variables g , a , c and t get the data from that file 4)变量g , a , c和t从该文件获取数据
5) If the sequence doesn't exist, print an error... 5)如果序列不存在,请打印错误...
As far as I understand, to do all that, I just need to declare the variables, assign the .txt files to each one of them, and make an if/else... 据我了解,要做所有这些,我只需要声明变量,将.txt文件分配给每个变量,然后进行if / else ...
The problem is that I have tried all that I could find, and nothing works... 问题是我尽了所有可能的努力,却无济于事...
Obviously I am not asking to make the code for me, but... Can you guys at least tell me where do I need to start? 显然我不是要为我编写代码,而是...你们至少可以告诉我我需要从哪里开始? My logic for what I need to do is correct?
我的逻辑是正确的吗? I am missing something?
我想念什么吗?
I think you want this: 我想你想要这个:
import os
id = raw_input('please enter the file numbers id:')
file='AY{0}1.txt'.format(id)
if not os.path.exists(file):
print "Error the file doesn't exists"
else:
g,a,c,t=0,0,0,0
with open(file,'r') as f:
next(f)
for line in f:
for char in line.lower():
if char == 'g':
g+=1
if char == 'a':
a+=1
if char == 'c':
c+=1
if char == 't':
t+=1
print "Guanina: {0}".format(g)
print "Adenina: {0}".format(a)
print "Citosina: {0}".format(c)
print "Timina: {0}".format(t)
gc = (g+c+0.) / (a+t+c+g+0.)
print "Conteúdo GC: {0}".format(gc)
But I think that you should explain the expected behavior of your code because it is not really clear. 但是我认为您应该解释代码的预期行为,因为它并不清楚。
The problem I see in your code is that you only read one line from the text file. 我在您的代码中看到的问题是,您只能从文本文件中读取一行。 The code below will return a list of the entire document so you could iterate upon that the way the rest of your code does.
下面的代码将返回整个文档的列表,因此您可以按照其余代码的方式进行迭代。
with open("AY365046.1.txt","r") as f:
lines = f.readlines()
You can read more about the file object in the manual 您可以在手册中阅读有关文件对象的更多信息。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.