Python函数根本不运行?

2024-09-19 23:35:51 发布

您现在位置:Python中文网/ 问答频道 /正文

我对编程世界相当陌生,我很想知道为什么下面的代码不仅拒绝运行,而且我的python软件甚至不会给我任何错误消息。我无法让应用程序运行此代码(它不会给我任何错误消息),我想知道这是我的代码本身,还是只是应用程序。如能了解这一问题,我们将不胜感激。在

def starBits():
    badMatchups = [Zelda, Cloud, Ryu]
    worstMatchups = [Jigglypuff, Villager, Bayonetta]
    print(badMatchups)[1:2]
    print(worstMatchups)[1:1]

def main():
    starBits()

main()

Tags: 代码程序运行应用程序消息软件maindef编程
1条回答
网友
1楼 · 发布于 2024-09-19 23:35:51

我不知道你对此有什么期望,但它的语法真的很时髦。在

print(badMatchups)[1:2]
print(worstMatchups)[1:1]

如果这些片段是列表的下标,则需要在调用print时使用它们:

^{pr2}$

顺便问一下,你知不知道[1:1]是一片空白?第二个数字是第一个包含的位置。你可能需要

print(badMatchups[1:3])     # two elements
print(worstMatchups[1:2])   # one element

另外,这些元素是外部变量,还是应该是字面名称?如果是后者,那你就得把它们用引号引起来。在

badMatchups = ["Zelda", "Cloud", "Ryu"]
worstMatchups = ["Jigglypuff", "Villager", "Bayonetta"]

有了这个变化,代码就可以运行了;我希望这是您想要的。在


不能让它运行吗?现实检查时间。。。在

完整代码,所做的更改

def starBits():
    badMatchups = ["Zelda", "Cloud", "Ryu"]
    worstMatchups = ["Jigglypuff", "Villager", "Bayonetta"]
    print(badMatchups[1:3])
    print(worstMatchups[1:2])

def main():
    starBits()

main()

输出:

['Cloud', 'Ryu']
['Villager']

相关问题 更多 >