Python函数不会运行吗?

时间:2016-09-22 23:00:31

标签: python python-3.x

我是编程世界的新手,我想知道为什么下面的代码不仅会拒绝运行,而且我的python软件甚至不会给我任何错误消息。我正在使用Python的IOS应用程序蟒蛇。我无法让应用程序运行此代码(它不会给我任何错误消息),并想知道它是我的代码本身,还是只是应用程序。任何有关这个问题的知识都将不胜感激。

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

def main():
    starBits()

main()

1 个答案:

答案 0 :(得分:3)

我不确定您对此的期望,但它的非常时髦的语法。

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

如果这些切片是列表的下标,则在打印的调用中需要它们:

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

顺便问一下,你是否意识到[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']