如何跳入 Python 中的行?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4480926/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to jump into lines in Python?
提问by altin
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
编辑:从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'
回答by Utku Zihnioglu
There this librarythat allows goto in Python but please, don't use it.
有此库,允许转到在Python,但请不要使用它。
回答by arboc7
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'
回答by Katriel
No, there is no such command. It is known as a gotoand pretty much only occured in very early programming language. It is never necessary: you can always achieve the same effect with a combination of ifand while(or, more Pythonically, for), and considered harmfulby many.
不,没有这样的命令。它被称为 agoto并且几乎只出现在非常早期的编程语言中。这是从来没有必要:你总是可以达到同样的效果与组合if以及while(或者,更Pythonically, for),并认为是有害的很多。
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.
它经常被滥用的原因是它使程序的流程难以遵循。在阅读普通(结构化)程序时,很容易判断控制流向何处:围绕 while 循环、进入方法调用或按条件拆分。goto但是,当使用 读取程序时,控件可以在程序周围任意跳转。
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 database1and not x in database1?)
(PS 你确定你的意思[x] in database1不是x in database1吗?)
EDIT: Here's an edited version of the code you put into your pastebin:
编辑:这是您放入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!'
回答by Hetal Darji
Best Example for Substitute GOTO function in Python
在 Python 中替换 GOTO 函数的最佳示例
def L1():
定义 L1():
a = 10
print(a)
def L2():
定义 L2():
b = 100
print(b)
var = str(input("Give your choice, True, False?"))
var = str(input("给出你的选择,对,错?"))
if var == 'True': L1()
如果 var == 'True': L1()
else :
别的 :
L2()

