raw_input and python print do not work in functions

Asked

Viewed 357 times

1

Good people I’m starting now in python and I have to create this project of a quiz that completes the spaces of the sentences. But my difficulty is in understanding why the raw_inputs and print are not working within the functions. Could someone give me a hint? My code is like this:

#coding = utf-8
data = {
'facil': {
    'frase': 'Chuva _1_ . _2_ . _3_ . e _4_ deitado.',
    'respostas': ['cai', 'em', 'pe', 'corre'],
    'falhas': 5
},
'medio': {
    'frase':
    'Quando falamos de Google _1_, _2_ Drive, _3_ e _4_; nos referimos a aplicativos de armazenamento em nuvem(cloud computing).\n',
    'respostas': ['Drive', 'One', 'Dropbox', 'Mega'],
    'falhas':
    2
},
'dificil': {
    'frase':
    'Sabendo que x é igual a 7 e ele segue repetindo por mais três vezes e cada repetição soma se 6 e na ultima subtrai 2, concluimos que o segundo valor é: _1_, o terceiro valor é _2_, para o quarto resultamos em _3_ e para finalizar temos _4_ sendo a soma dessa sequência \n.',
    'respostas': ['13', '19', '23', '62'],
    'falhas':
    0
 }
}
def nivel():
while True:
  nivel = raw_input('Escolha entre o nivel facil, medio e dificil').lower()
  if nivel in data:
        return nivel
  print 'Esse nivel nao existe tente novamente'


def jogada(nivel): # Ao digitar o nivel ele é passado para essa função
frase = data[nivel]['frase']
chances = data[nivel]['falhas']
index = 0

  while index <len(data[nivel]['respostas']) and chances >=0:
     print frase
     respostas = raw_input(['frase']+str(index + 1 +'\n')).lower()
  if respostas == data[nivel]['respostas']:
     print 'Correto.'
     frase = frase.replace(str(index +1)).data[nivel]['repostass'][index] #Troca os espaços em brancos por palavra digitadas se forem corretas
  • Some error is presented?

  • No error, just shows no information.

  • Is this your entire program? You have defined two functions but in no time are you calling them.

  • Thanks for the tip. You Sveen and Giovanni Nunes helped me a lot, the program started running.

1 answer

0

You need to call the functions, the most elegant way in Python is to create a main function():

def main():
    nivel_escolhido = nivel()
    jogada(nivel_escolhido)

if __name__ == "__main__":
    main()

So your duties will be performed.

Just letting you know there are some mistakes in the loop while of function jogada().

  • Thanks for the tip, I’ll try to do it that way.

  • But that’s what your logic has defined, it stays in a loop until a valid level is entered.

  • I ended up taking the last remark, sorry. Yes, my logic was set that way as you said, but even if you enter the right level it does not execute another part of the code, it was only in the level while.

  • Giovanni Nunes, I used def main as my guide, I changed the code to: def nivel(): nivel = raw_input('Choose between easy, medium and hard level'). Lower() while True: if nivel in data: modo(nivel) Else: print 'This level does not exist try again' break nivel() So solve the problem with while per hour, but I’ll solve the others first.

Browser other questions tagged

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