(python) as I put the go inside def

Asked

Viewed 49 times

-4

numero_sorte = 3
n_tentativas = 4

for rodada  in  range(1,n_tentativas):
  tentativa_2 = input ('qual o numero secreto? ')
  tentativa_2= int (tentativa_2)
  def jogo_adivinha(chute):
    if numero_sorte == chute:
      print('parabens vc acertou')
    elif numero_sorte < chute:
      print('o numero secreto e menor que isso')      
    elif numero_sorte > chute :
      print('o numero secreto e maior que isso')

that’s my code

  • Know the difference between a loop of repetition and a subroutine?

2 answers

0

Friend, follow what you want to do, for your doubt I put below and commented how to put the for within the function. Remembering that Python works with endentation, maybe not correctly endeared.

import sys

numero_sorte = 3
n_tentativas = 4

def jogo_adivinha(chute):
    if numero_sorte == chute:
      print('Parabens vc acertou')
      sys.exit()
    elif numero_sorte < chute:
      print('o numero secreto e menor que isso\n')
    elif numero_sorte > chute :
      print('o numero secreto e maior que isso\n')

for rodada  in  range(0,n_tentativas):
  tentativa = int(input ('Qual o numero secreto ? '))
  #chama a funcao
  jogo_adivinha(tentativa)

print('Você perdeu\n')

#For dentro da função    
'''

def Teste():
    for x in range(0,3):
        print('passou por aqui: '+str(x))

#Chama funcao
Teste()

'''

-1

Python is a language that has no block delimiters, such as C, so indentation is part of your code. In order for the code to be associated with a function, structure or loop it is necessary to give a tabulation space. To put the for in the function it is necessary to do as follows:

funcao():
    for i in valores:
        condicao

Browser other questions tagged

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