Ask coding questions

← Back to all posts
how do i make the name section run only when the edad if is run ??
GINITO2508 (1)

here is mi code

edad = int( input("¿cual es tu edad?"))
if edad >= 18:
print (" ")
print ("oye, eres mayor de edad, continua completando los datos")
print (" ")

else:
print (" ")
print ("oye, eres menor de edad")
print (" ")

name = input('¿¿como te llamas??\n')
print('hola %s!!' % name)

Answered by MadMath123 (54) [earned 5 cycles]
View Answer
Comments
hotnewtop
MadMath123 (54)

I don't fully understand what you mean -- it looks functional.

GINITO2508 (1)

@MadMath123 i need to be executed only when the if that says >= 18 is executed

LukeWright (114)

Just move it to line 6 then @GINITO2508

MadMath123 (54)

@GINITO2508 Indent the print commands, then put the 'name' code into the >18 part, like this:

edad = int( input("¿cual es tu edad?"))
if edad >= 18:
    print (" ")
    print ("oye, eres mayor de edad, continua completando los datos")
    print (" ")
    name = input('¿¿como te llamas??\n')
    print('hola %s!!' % name)
else:
    print (" ")
    print ("oye, eres menor de edad")
    print (" ")

Python does ifs, loops, and more with indents. The more indents something has, the more loops, ifs, etc. it's in. Sort of like {}, but for Python.

RYANTADIPARTHI (6001)

Solution

this is the correct code. Try this.

edad = int(input("¿cual es tu edad?"))
if edad >= 18:
    print (" ")
    print ("oye, eres mayor de edad, continua completando los datos")
    print (" ")

else:
    print (" ")
    print ("oye, eres menor de edad")
    print (" ")

    name = input('¿¿como te llamas??\n')
    print('hola %s!!' % name)   

like that.
That should work

robowolf (436)

You have to indent whatever code you want to be in the if or else statement.