Usually answers to more specific questions about programs already started, when you do not know where to go from a point.
Nevertheless, I'll try to give you some tips: the main functionality normally
neglected by beginners in programming, in any language, are "functions." You should have learned how to create and use one in Python (with the def
command).
From there, if you split every different thing your program has to do for a function - as small as possible, and join a main function to "orchestrate" the call from the others, things start to get simpler. / p>
Even for the "talk to the world" program - in this case, reading typed strings and printing answers, it uses functions. In this case, it is the Python builtin functions: print
and input
(you must have also learned).
With functions, the command for loop for
, and the command to execute code depending on a condition ( if
) can do any program. And I even talk about programs that serve web pages, design windows, games, etc ... - The only difference is that in those cases you will need to use functions (sometimes from third parties) to "talk" to the world differently. (Functions to create a text field in a window, and read what is typed there, instead of input
that only reads from the terminal, for example).
Of course, the Python language and all others will much in addition to def
, for
and if
- and this allows you to create shorter, more expressive, more organized programs. But with those 3 you do it all.
So in that case, you should start thinking about a skeleton in your program - and then what each function will do. If you do not quite understand how a function receives parameters and returns a result - this you need to study. I recommend using Python in interactive mode.
def ler_gabarito(comprimento, alternativas):
correto = True
entrada = input("Digite o gabarito: ")
if len(entrada) != comprimento:
print(f"São {comprimento} questões! Digite o número certo de respostas")
correto = False
for letra in entrada:
if letra not in alternativas:
print(f"A letra {letra} não é uma alternativa válida")
correto = False
if correto: # (nunca precisa fazer 'variavel == True')
return entrada
# se não estiver bom, chama a mesma função de novo e retorna seu resultado
return ler_gabarito(comprimento, alternativas)
def le_resposta(gabarito):
...
nota = 0
for i in range(len(gabarito)):
...
print(nota)
def principal():
gabarito = ler_gabarito(..., ...)
for i in range(5):
le_resposta(gabarito)
principal()