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

-1

Is it possible to do such a system in Python, let's suppose on 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 as the person put type like

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

I want to repeat this question and separate each one into a "string" to later put the media of everything and several other things, but the focus is this, I wanted to know if you have how to do this.     

asked by anonymous 26.01.2017 / 02:46

4 answers

4

I did not quite understand the part of the averages, but here is a solution to your problem storing issues:

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))
    
26.01.2017 / 10:53
3

If your intention is just to have a list of materials stored as a string, you just 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 stories in the series)

  

2

Entry of materials

  

Portugues
  Mathematics

Output of materials

  

Portugues
  Mathematics

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

Read more about lists .

    
26.01.2017 / 03:45
2

I do not know if I understand your question well, but I think you need a list of subjects and a dictionary for each subject. The result would look 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 mount this structure, I thought of a type 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 need a bigger work to do another loop to fill each student and the total average could be done with a quick calculation of the filled averages.

In addition, in your code, you can not store print () in a variable. First you store the input in the variable and then you give a print () command.

    
26.01.2017 / 03:25
0

If I understood correctly, you can do this:

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:

  

Type the exact number of stories in this series: 2

     

Type the name of the article: Mathematics

     

Type the name of the article: English

     

['Mathematics', 'English'] #contents array array

    
26.01.2017 / 11:55