2010-12-19 18 views
0

我想知道是否有一個命令,這將有助於我去一個特定的行並跳過其他線路,類似如下:如何跳入Python中的行?

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 

編輯:添加代碼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, Than your free to go ' + name + ' :)' 

if [x] in database1: print ' Thank you ' + name + ' :)\n' 
if [x] in database1: print 'Try (No) next time !\n' 

回答

1

還有this library它允許在Python中跳轉,但請不要使用它。

+0

你能告訴我爲什麼不應該使用它嗎? – Amir 2016-08-08 06:28:21

+0

來自項目主頁:'「goto」模塊是2004年4月1日發佈的愚人節玩笑。是的,它可以工作,但這是一個笑話。請不要在真正的代碼中使用它' – 2016-09-04 19:10:43

0

如果你只是在調試,在我看來,最簡單的方法就是暫時將這些行註釋掉。只需在要跳過的所有行的開頭添加#即可。

if [x] in database1: 
    code to execute 

# code to skip 

if [x] in database1: print ' Thank you ' + name + ' :)\n' 
3

不,沒有這樣的命令。它被稱爲goto,幾乎只發生在非常早的編程語言中。它是never necessary:您可以始終使用ifwhile(或更多Python語言,for)和considered harmful的組合獲得相同的效果。

它被濫用的原因是它使程序的流程難以遵循。當閱讀一個正常的(結構化的)程序時,很容易知道控制權在哪裏流動:圍繞一個while循環,進入一個方法調用或者被一個條件分割。但是,使用goto讀取程序時,控件可以隨意在程序中跳轉。

在你的情況,你既可以附上所有的中間線中使用條件,否則重構第二行到一個單獨的功能:

def thank(x, name): 
    if [x] in database1: 
     print 'Thank you, {0}:\n'.format(name) 

(PS你確定你的意思[x] in database1而不是x in database1? )


編輯:這裏是代碼的編輯版本,你把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!' 
+0

maby這將有助於: – altin 2010-12-19 00:32:34

+0

也許這將有助於:http://pastebin.com/MY55yBbE – altin 2010-12-19 00:40:04

+0

@altin:我已經從你的pastebin複製代碼到這個問題,所以他們相關聯。總之,答案是:使用'else'! – katrielalex 2010-12-19 00:48:57