James Diego Tao James Diego Tao - 2 months ago 5
Python Question

Python Function Won't Run At ALL?

I am fairly new to the programming world and am wondering why the following code will not only refuse to run, but my python software won't even give me any error messages whatsoever.I'm using Pythonista, an IOS app for python. I can't get the app to run this code (and it won't give me any error messages) and was wondering if it's my code itself, or is it just the application. Any knowledge on this issue would be greatly appreciated.

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

def main():
starBits()

main()

Answer

I'm not sure what you expect from this, but it's really funky syntax.

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

If those slices are subscripts for the lists, you need them inside the call to print:

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

By the way, do you realize that [1:1] is an empty slice? The second number is the first position not included. You may need

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

Also, are those elements external variables, or are they supposed to be literal names? If the latter, then you have to put them in quotation marks.

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

With this change, the code runs; I hope it's what you want.


Can't get it to run? Reality check time ...

Full code, changes made:

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

def main():
    starBits()

main()

Output:

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