How do i return to the previous question in Python Script
Question:
i’m new on using Python Script, i’ve been using Jupyter Notebook and finally realized the differences between the two of them. In this code i’m trying to create a simple command using def, where i need to type the number 1 – 3.
def Twitter():
while True:
user_input = input(
'Pick one: 1) Crawling | 2) Verification | 3) Return ---->')
if user_input == '1':
print('You picked Crawling')
break
elif user_input == '2':
print('You picked Verification')
break
elif user_input == '3':
print('Return')
Opening()
break
else:
print('Type a number 1-3')
continue
user_input = ''
def Opening():
while True:
user_input = input(
'Pick one: 1) Twitter | 2) Instagram ---->')
if user_input == '1':
Twitter()
break
elif user_input == '2':
print('Instagram!')
break
else:
print('Type a number 1-2')
continue
I’m still very immature and I need help on when I picked the number 3, the function would return to the previous part instead of not outputing the result at all thanks!
Answers:
Instead of using a break
statement, you can return the function inside a function.
See: https://www.geeksforgeeks.org/returning-a-function-from-a-function-python/
def Twitter():
while True:
user_input = input(
'Pick one: 1) Crawling | 2) Verification | 3) Return ---->')
if user_input == '1':
print('You picked Crawling')
break
elif user_input == '2':
print('You picked Verification')
break
elif user_input == '3':
print('Return')
return Opening() # switch to Opening()
else:
print('Type a number 1-3')
continue
def Opening():
while True:
user_input = input(
'Pick one: 1) Twitter | 2) Instagram ---->')
if user_input == '1':
return Twitter() # switch to Twitter()
elif user_input == '2':
print('Instagram!')
break
else:
print('Type a number 1-2')
continue
if __name__ == '__main__':
Opening()
it works fine for me…
maybe you can try to call the function recursively instead of using while true?
def Twitter():
user_input = input(
'Pick one: 1) Crawling | 2) Verification | 3) Return ---->')
if user_input == '1':
print('You picked Crawling')
Twitter()
elif user_input == '2':
print('You picked Verification')
Twitter()
elif user_input == '3':
print('Return')
Opening()
else:
print('Type a number 1-3')
Twitter()
user_input = ''
def Opening():
user_input = input(
'Pick one: 1) Twitter | 2) Instagram ---->')
if user_input == '1':
Twitter()
elif user_input == '2':
print('Instagram!')
Opening()
else:
print('Type a number 1-2')
Opening()
Twitter()
i’m new on using Python Script, i’ve been using Jupyter Notebook and finally realized the differences between the two of them. In this code i’m trying to create a simple command using def, where i need to type the number 1 – 3.
def Twitter():
while True:
user_input = input(
'Pick one: 1) Crawling | 2) Verification | 3) Return ---->')
if user_input == '1':
print('You picked Crawling')
break
elif user_input == '2':
print('You picked Verification')
break
elif user_input == '3':
print('Return')
Opening()
break
else:
print('Type a number 1-3')
continue
user_input = ''
def Opening():
while True:
user_input = input(
'Pick one: 1) Twitter | 2) Instagram ---->')
if user_input == '1':
Twitter()
break
elif user_input == '2':
print('Instagram!')
break
else:
print('Type a number 1-2')
continue
I’m still very immature and I need help on when I picked the number 3, the function would return to the previous part instead of not outputing the result at all thanks!
Instead of using a break
statement, you can return the function inside a function.
See: https://www.geeksforgeeks.org/returning-a-function-from-a-function-python/
def Twitter():
while True:
user_input = input(
'Pick one: 1) Crawling | 2) Verification | 3) Return ---->')
if user_input == '1':
print('You picked Crawling')
break
elif user_input == '2':
print('You picked Verification')
break
elif user_input == '3':
print('Return')
return Opening() # switch to Opening()
else:
print('Type a number 1-3')
continue
def Opening():
while True:
user_input = input(
'Pick one: 1) Twitter | 2) Instagram ---->')
if user_input == '1':
return Twitter() # switch to Twitter()
elif user_input == '2':
print('Instagram!')
break
else:
print('Type a number 1-2')
continue
if __name__ == '__main__':
Opening()
it works fine for me…
maybe you can try to call the function recursively instead of using while true?
def Twitter():
user_input = input(
'Pick one: 1) Crawling | 2) Verification | 3) Return ---->')
if user_input == '1':
print('You picked Crawling')
Twitter()
elif user_input == '2':
print('You picked Verification')
Twitter()
elif user_input == '3':
print('Return')
Opening()
else:
print('Type a number 1-3')
Twitter()
user_input = ''
def Opening():
user_input = input(
'Pick one: 1) Twitter | 2) Instagram ---->')
if user_input == '1':
Twitter()
elif user_input == '2':
print('Instagram!')
Opening()
else:
print('Type a number 1-2')
Opening()
Twitter()