How to jump into lines in Python?
I want to know if there is a command that would help me go to a specific line and skip other lines, something like the following:
if [x] in database1: (the command I need)
skipping other lines.......
to go here:
if [x] in database1: print ' Thank you ' + name + ' :)\n' <<<< which is in line 31
EDIT: added code from pastebin
print ' Note: Write your Name and Surname with the first leter, BIG !'
print ' ...'
name = raw_input('Name: ')
surname = raw_input('Surname: ')
print name + ' ' + surname + ',' + ' The Great !\n'
print ' Did you like this ?'
x = raw_input('Yes/No : ')
print ''
database = [
['No'],
['no']
]
database1 = [
['Yes'],
['yes']
]
if [x] in database: print ' Did you really meant that ?'
if [x] in database: y = raw_input('Yes/No : ')
# (I need the command here to go that line :)
if [y] in database1: print ' So you'll be dead ' + name + ' !\n'
if [y] in database: print ' Oh OK, T开发者_开发技巧han your free to go ' + name + ' :)'
if [x] in database1: print ' Thank you ' + name + ' :)\n'
if [x] in database1: print 'Try (No) next time !\n'
No, there is no such command. It is known as a goto
and pretty much only occured in very early programming language. It is never necessary: you can always achieve the same effect with a combination of if
and while
(or, more Pythonically, for
), and considered harmful by many.
The reason it is oft abused is that it makes the flow of the program difficult to follow. When reading a normal (structured) program it is easy to tell where control will flow: either around a while loop, into a method call, or split by a conditional. When reading a program using goto
, though, the control can jump arbitrarily around the program.
In your case, you could either enclose all the intermediate lines inside a conditional, or else refactor the second line into a separate function:
def thank(x, name):
if [x] in database1:
print 'Thank you, {0}:\n'.format(name)
(P.S. Are you sure you mean [x] in database1
and not x in database1
?)
EDIT: Here's an edited version of the code you put into your pastebin:
print 'Enter your name and surname:'
# `.title()` makes first letter capital and rest lowercase
name = raw_input('Name: ').title()
surname = raw_input('Surname: ').title()
# use `.format(...)` to create fancy strings
print '{name} {surname}, the Great!'.format(name=name, surname=surname)
noes = ['no', 'n']
yesses = ['yes', 'y']
print 'Did you like this?'
# `.lower()` for lowercase
if raw_input('Yes/No: ').lower() in noes:
print 'Did you really mean that?'
if raw_input('Yes/No : ') in yesses:
print 'So you\'ll be dead, {name}!'.format(name=name)
else:
print 'Oh, OK, then you\'re free to go, {name}.'.format(name=name)
else:
print 'Thank you, {name}.'.format(name=name)
print 'Try "no" next time!'
Best Example for Substitute GOTO function in Python
def L1():
a = 10
print(a)
def L2():
b = 100
print(b)
var = str(input("Give your choice, True, False?"))
if var == 'True':
L1()
else :
L2()
If you're just debugging, the easiest way to skip lines, in my opinion, is to comment out those lines temporarily. Just add #
at the beginning of all the lines you want to skip.
if [x] in database1:
code to execute
# code to skip
if [x] in database1: print ' Thank you ' + name + ' :)\n'
精彩评论