简体   繁体   English

如何要求用户输入使代码交互

[英]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.txtsequence2.txt )。 ..然后,获取由input()函数通知的序列/文件中gact的数据...

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)变量gact从该文件获取数据

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.

相关问题 如何让程序要求用户输入长度并使用输入来生成密码,只要用户愿意 - how to make the program ask the user for a input for length and use the input to make the password as long as the user would like 如何让一个 python 文件要求用户输入,然后将此输入传递给第二个 .py 文件? - How can I make one python file ask for user input and then pass this input to a second .py file? 我如何要求用户输入时间 - How do I ask the user to input time 如何使用 Python 类并要求用户输入? - How to use a Python class and ask for user input? 如何要求用户在python 3中输入函数 - How to ask a user to input a function in python 3 在绘制线性方程时,如何让我的程序询问用户输入 - How to make my program ask for user input when graphing linear equations 如何让 python 在 def main() 中只要求用户输入一次? - How do I make python only ask for user input once in def main()? 如何询问用户是否希望代码重新开始 - how to ask the user if they want the code to start again 如果用户在我的 if 语句后回答“否”,我如何才能让代码再次询问您的姓名? - How do I make it so that the code will ask you your name again if the user answers no after my if statement? 在用户停止之前,我如何要求用户输入? - How do I ask for user input until the user stops?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM