-4

I am using python 3, and I want to know how to return the code to the second linex = input ('Enter your first name here: ') after completing the if or elif conditions

I have searched other stack overflow answers but to no avail

print ("hello world")
x = input ('Enter your first name here: ')
if len(x) > 10:
    print ("You have a long name")
elif:
    print ("You have a short name")
taurus05
  • 2,491
  • 15
  • 28
  • What do you mean by return the code to the second line? Do you want to print the name after if-elif block? – min2bro Jul 03 '19 at 05:02
  • 2
    Loops are covered in the very first part of the Python tutorial "First steps towards programming": https://docs.python.org/3/tutorial/introduction.html#first-steps-towards-programming (in your case, if you want to return indefinitely until the user aborts the program, you can use a `while True:` loop. – Tim Pietzcker Jul 03 '19 at 05:02

2 Answers2

1

Replace elif with else

The elif is short for else if. It allows us to check for multiple expressions. You aren't checking anything hence it should be replaced with else

Plus by doing that you fix a syntax error

    elif:
        ^
SyntaxError: invalid syntax

This is not a valid Python statement.

edit:

if you want to repeat the question askeed to the user use a while loop:

in your case:

while True:
    x = input ('Enter your first name here: ')
    if len(x) > 10:
        print ("You have a long name")
    else:
        print ("You have a short name")

This will work forever unless you kill it via a Task Manager or interrupt it using the keyboard.

MatejMecka
  • 1,448
  • 2
  • 24
  • 37
  • this is not what OP has asked! – taurus05 Jul 03 '19 at 05:03
  • @taurus05 What exactly *did* OP ask? ‍♂️ – deceze Jul 03 '19 at 05:04
  • 2
    @deceze, OP wants to jump to this line 2, `x = input ('Enter your first name here: ')`, after the if and elif conditions are done. This is similar to the behaviour of goto, which is not supported in python. – taurus05 Jul 03 '19 at 05:09
  • Updated @taurus05 – MatejMecka Jul 03 '19 at 05:14
  • @deceze, this is not a duplicate. The link that you've shared is about moving back to the first line of code. What if, there are 10 lines in the code and the pointer is to be moved on to the 4th line of the code, which doesn't require a loop? – taurus05 Jul 03 '19 at 05:16
  • @taurus05 I expect OP to be able to figure that out, once they know `while True`. – deceze Jul 03 '19 at 05:18
  • @deceze, seems fine then. If this is not what OP is expecting, marking it as duplicate won't make any sense. – taurus05 Jul 03 '19 at 05:20
  • 1
    @taurus05 https://stackoverflow.com/questions/438844/is-there-a-label-goto-in-python – MatejMecka Jul 03 '19 at 05:22
0

You can use loop statement to return the second check condition again. Please refer this link for loops in python

for example of using while loop

answer = None
while True:
answer = raw_input("Do you like pie?")
if answer in ("yes", "no"): break
print "That is not a yes or a no"

it works like goto statement. you can use break or continue statement to stop or continue execution of loop.

Thayif kabir
  • 715
  • 5
  • 20