如果运行Python代码然后调用exit(),它将退出程序并且以下代码将不会运行。但是我添加exit()到我的程序中,然后出现一些语法错误并且程序崩溃了。我想知道为什么 Python 编译器在运行之前没有优化我的代码。我用逻辑错误尝试了这个问题,但它忽略了它们,比如index out of range等等。那么为什么下面的代码不起作用并SyntaxError发生呢?
print("Hi")
exit()
if
Run Code Online (Sandbox Code Playgroud) 这是最奇怪的事情。我的 python 刚刚停止处理其构造函数中具有多个参数的类?运行 python 3.8.10 出现错误TypeError: Person() takes 1 positional argument but 2 were given
def Person(object):
def __init__(self, a, b):
self.aa = a
self.bb = b
pp = Person(20, 40)
Run Code Online (Sandbox Code Playgroud)
如果我将 Person__init__降低到一个参数,那么它就会起作用。如果我将其提高到 3,则会出现相同的takes 1 but 3 were given错误。我完全被难住了?
我正在尝试在列表中使用 f 字符串。我创建了一个新的行变量。标准变量工作正常,但新行变量不行
NL = '\n'
var = 'xyz'
lst = []
print(f'test{NL}new line')
lst.append(f"first line var is {var}{NL}a second line {NL}")
lst.append(f"third line{NL}forth line var is {var}")
print(lst)
Run Code Online (Sandbox Code Playgroud)
创建输出
test
new line
['first line var is xyz\na second line \n', 'third line\nforth line var is xyz']
Run Code Online (Sandbox Code Playgroud)
我怎样才能做到这一点?
import re
grades = 'AAABBBACBACCCCCAABCABACCCCAABCBBCA'
re.findall('(?:a{3})*', grades)
Run Code Online (Sandbox Code Playgroud)
在这里我想找到所有是三个“a”字符的倍数的字符串,但结果是一个列表'',即
['', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '']
该语法来自重新文档
要将第二次重复应用于内部重复,可以使用括号。例如,表达式
(?:a{6})*匹配六个“a”字符的任意倍数。
我不知道哪里错了,你能帮我吗?
我希望是这样,['AAA']但结果显然不是。
在 Python 中,这是有效的:
for site in userSites:
for role in site.roles:
if role.role == Role.ADMIN:
return True
return False
Run Code Online (Sandbox Code Playgroud)
我正在寻找一种在一行中执行此操作的方法,该方法可以检查嵌套数组以查看是否有任何用户站点包含管理员角色。
如果我有大约 10 个命令,并且它们都有特定的用途,因此无法修改它们,但我想将它们放入列表中而不调用它们。
def print_hello():
print("hello")
command_list = [print_hello()]
Run Code Online (Sandbox Code Playgroud)
这只会打印"hello",然后留下command_list等于[None]
我怎样才能得到它,以便当我打字时command_list[0]它会执行print_hello()?
我的问题是这样的,但我不明白。如何将函数调用添加到列表中?
if后面是thenbash,但我不明白为什么then不能在同一行if [...] then中使用它,因为必须在下一行中使用它。这样可以消除代码中的歧义吗?或bash是这样设计的?其根本原因是什么?
我试着写if,并then在同一行,但它给了以下错误:
./test: line 6: syntax error near unexpected token \`fi'
./test: line 6: \`fi'
Run Code Online (Sandbox Code Playgroud)
代码是:
#!/bin/bash
if [ $1 -gt 0 ] then
echo "$1 is positive"
fi
Run Code Online (Sandbox Code Playgroud) 我不明白这怎么可能?这里我在循环外使用ifor循环的值。forfor
for i, kv in enumerate(bucket):
k, v = kv
if key == k:
key_exists = True
break
#here is the issue...
if key_exists:
bucket[i] = ((key, value))
print(i)
else:
bucket.append((key, value))
Run Code Online (Sandbox Code Playgroud) 我最近开始使用 python 并尝试编写简单的程序,但是这个错误一直出现。我究竟做错了什么?
class Store:
def __init__(self,name, id, price):
self.name=name
self.id=id
self.price=price
print("------------")
Store()
item1=Store(Chips, 10, 500)
Run Code Online (Sandbox Code Playgroud)
输出
class Store:
def __init__(self,name, id, price):
self.name=name
self.id=id
self.price=price
print("------------")
Store()
item1=Store(Chips, 10, 500)
Run Code Online (Sandbox Code Playgroud)
提前致谢。
def count_match(data,target):
count = 0
for i in data:
if i == target:
count += 1
return count
count_match([1,2,3,5,6,5,6,6,6,4],6)
[Finished in 0.6s]
Run Code Online (Sandbox Code Playgroud)
预期输出:4
为什么 return 语句在 Sublime Text 编辑器中不起作用?
python ×9
python-3.x ×2
bash ×1
compilation ×1
f-string ×1
for-loop ×1
if-statement ×1
list ×1
regex ×1
sublimetext3 ×1