我理解这个结构是如何工作的:
for i in range(10):
print(i)
if i == 9:
print("Too big - I'm giving up!")
break;
else:
print("Completed successfully")
Run Code Online (Sandbox Code Playgroud)
但我不明白为什么else在这里用作关键字,因为它表明有问题的代码仅在for块未完成时运行,这与它的作用相反!无论我如何看待它,我的大脑都无法从for语句无缝地进展到else块.对我来说,continue或者continuewith更有意义(我正在努力训练自己阅读它).
我想知道Python编码器如何在他们的脑海中读取这个结构(或者如果你愿意的话,大声朗读).也许我错过了一些会让这些代码块更容易破译的东西?
我注意到以下代码在Python中是合法的.我的问题是为什么?有具体原因吗?
n = 5
while n != 0:
print n
n -= 1
else:
print "what the..."
Run Code Online (Sandbox Code Playgroud) 我自学了python 2.7.我有使用BATCH的经验,它有一个GOTO语句.我怎么在python中做到这一点?例如,假设我想从第5行跳到第18行.
我意识到之前有关于这个主题的问题,但我没有发现它们足够的信息,或者在我目前的理解中python级别太高.
我必须goto在Python中使用.我发现了托管,goto但我的Python实现(Mac上的CPython 2.7.1)没有这个模块,所以它似乎不是可移植的.它应该至少适用于支持CPython字节码的所有Python实现(特别是我关心CPython和PyPy).然后有这个相关的问题,和cdjc的goto.以下是答案给出的.
我可以手动构建字节码(即编写我自己的Python编译器)因为有这样的指令(JUMP_ABSOLUTE和朋友).但我想知道是否有更简单的方法.是否可以通过inspect左右来调用单个字节码指令?我还想过通过Python编译然后自动修补生成的Python字节码.
当然,如果我不解释为什么我真的需要这个,人们会问为什么,不会给我任何有用的答案.所以简而言之我的用例:我正在将一个C AST翻译成Python AST并编译它.我可以用某种方式将每个逻辑流(所有循环和其他东西)映射到等效的Python代码.除了之外的一切goto.相关项目:PyCParser(参见参考资料interpreter.py),PyCPython,PyLua.
我最近读到了这个问题,它有一个关于在Java中标记循环的解决方案.
我想知道Python中是否存在这样的循环命名系统.我曾多次遇到过需要for从内for循环中突破外循环的情况.通常,我通过将内部循环放在一个返回(以及其他)布尔值的函数中来解决这个问题,该布尔值用作断开条件.但是标记循环的中断似乎要简单得多,我想尝试一下,如果python中存在这样的功能
有谁知道它是否存在?
很抱歉,代码段已被删除.我正在分裂这个问题
在 Python 程序中,通常使用 try-except 块捕获异常:
try:
# Do stuff
except ValueError:
# Handle exception
Run Code Online (Sandbox Code Playgroud)
据我所知,在异常处理程序中捕获异常的最佳方法是嵌套的 try-except 块。然而,对于许多嵌套的 try-catch 块,这可能会变得有点混乱:
try:
# Some assignment that throws an exception if the object is not in the list
try:
# Some assignment function that throws an exception if the the object is not already in the database
# Error Handling
except ValueError:
try:
# Some function that throws an exception if the object does not have an undesired property
# Error Handling
except …Run Code Online (Sandbox Code Playgroud) 我对 Python 完全陌生,目前只是在学习字符串和变量。我知道你可以在代码前加上一个“#”来注释它,但是 Python 是否有一个选项可以完全忽略部分代码并从这段代码之后的行开始工作?
例如:
old_price = 30
new_price = 25
difference = old_price - new_price
name = "John Smith"
age = 15
print(name, age)
Run Code Online (Sandbox Code Playgroud)
我希望代码从行名称 =“John Smith”开始工作,而不必注释前 3 行。
所以我一直在尝试为我的firend做一个python程序,我做了(加入了两个不同的密码),并且我一直试图让每次打开和关闭程序时都很容易加密不同的字符串.我在64位Windows 7计算机上使用python 3.2.
这是我的代码(请同时给我一些改进的提示):
#!/usr/bin/python
#from string import maketrans # Required to call maketrans function.
print ("Welcome to the Rotten Bat encription program. Coded in python by Diego Granada")
answer = input("Please enter the password: ")
if answer == 'raindrops':
print("Password accepted")
else:
print ("Incorrect password. Quiting")
from time import sleep
sleep(3)
exit()
intab = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"
outtab = "N9Q2T1VWXYZ7B3D5F8H4JK60n9pq2st1vwxyz7b3d5f8h4jk60"
message = input("Put your message here: ")
print(message.translate(dict((ord(x), y) for (x, y) in zip(intab, outtab))))
print ("Thank you for using this …Run Code Online (Sandbox Code Playgroud) python ×8
break ×2
for-loop ×2
goto ×2
if-statement ×2
bytecode ×1
compilation ×1
continue ×1
exception ×1
for-else ×1
java ×1
label ×1
loops ×1
syntax ×1
while-loop ×1