Python, How to let the person put a number and with this number put that same amount of questions

Asked

Viewed 810 times

-1

Is it possible to make a system like this in Python, like let’s assume in a college system, if I put

materias = print(input("Digite o número exato de matérias que há nesta série: "))

what it takes to repeat the same amount that the person put like this

print(input("Digite o nome da matéria: "))

and I want to repeat this question and separate each one into a "string" to then put the media of everything and various other things , but the focus is this, I wanted to know if it has how to do this.

  • Java or python?

  • python..........

  • the question editing is something common here in the OS, I think there is no need to edit the question thanking someone for editing the question. : s

  • Okay then! .....

  • From what I understand you want to store a list of subjects as string, would that?

  • yes, and that....

  • 2

    As you have already noticed by the answers you had: yes, it is possible. But your question is not very good for the format of this site because it is too wide. You can do what you want in countless ways, including creating graphical interfaces (which would be more appropriate for a large amount of input data). I suggest specifying the question further (if your question is how to loop to read multiple console entries, do that one question directly).

Show 2 more comments

4 answers

4


I didn’t quite get the medium part, but here’s a solution to your material storage problem:

materias = []
qtd_materias = None
while not isinstance(qtd_materias, int):
    try:
        qtd_materias = int(input("Digite o numero exato de materias que ha nesta serie: "))
    except:
        print('Por favor digite um inteiro')

for _ in range(qtd_materias):
    materias.append(input("Digite o nome da materia: "))

print('As materias sao:', ', '.join(materias))
  • Nice! but there will only be everything on the material part has to separate?

  • Split how? I don’t see how you want to split @Genezis. Give an example?

  • If it’s meant to be text, I’ve already edited the answer to that @Genezis

3

If your intention is just to have a list of materials stored as a string, you will only need to use a list, and Python has a certain ease regarding list manipulation.

See an example based on what you described:

i = 0
materias = []
quantidadeMaterias = input("Digite o numero exato de materias que ha nesta serie: ")

while i < quantidadeMaterias:
    materia = raw_input("Digite a descricao da materia: ")
    materias.append(materia)
    i += 1

for m in materias:
    print(m)

Entry (Number of materials in the series)

2

Entry of materials

Portugues
Matematica

Removal of materials

Portugues
Matematica

I just needed to create a list that is the variable materias and then populated it through the loop.

Read more about lists.

  • Traceback (Most recent call last): line 5, in <module> while i < quantityMaterias: Typeerror: '<' not supported between instances of 'int' and 'str'

  • Just convert the input() return to int: quantidadeMaterias = int(input(...))

2

I’m not sure I understand your question, but I think you need a list of subjects and a dictionary for each subject. The result would be something like this:

    [{"nome": "Matemática",
          "serie": "quinto ano",
          "professor": "Paulo",
          "alunos":[{"nome": "Maria Antônia", "media": 7}, {"nome":"Renato", "media": 8}],
          "mediaTotal":7.5},

     {"nome": "Matemática",
          "serie": "sexto ano",
          "professor": "Rosa",
          "alunos":[{"nome": "Suzana", "media": 7}, {"nome":"Claudia", "media": 8}],
          "mediaTotal":7.5}]

To build this structure, I thought of a code like this:

    nMaterias = int(input("digite o número exato de matérias que há nessa série"))
    materias = []
    for i in range(nMaterias):
        materia = {
                    "nome": input("Qual o nome da matéria %s: " %(i+1)),
                    "serie": input("Qual a série da matéria %s: " %(i+1)),
                    "professor": input("Qual o nome do professor da matéria %s: " %(i+1)),
                    "alunos": None,
                    "mediaTotal": input("Qual a média da matéria %s: " %(i+1)),
                    }
        materias.append(materia)
    print (materias)

Well, of course it would take a bigger job to make another loop to fill each student and the total average could be done with a fast calculation of the filled averages.

Also, in your code, you cannot store print() in a variable. First you store the input in the variable and then you give a print command().

0

If I understand correctly, you can do so:

materias = []
n_materias= int(input("Digite o número exato de matérias que há nesta série: "))
for i in range(n_materias):
     materias.append(input("Digite o nome da matéria: "))
print(materias)

If you run the code it will do something like this:

Enter the exact number of subjects in this series: 2

Enter the name of the article: Mathematics

Enter the name of the article: English

['Mathematics', 'English'] #contents of the material array

Browser other questions tagged

You are not signed in. Login or sign up in order to post.