-1

Here is my code:

print "How many frames do you want?"
f = raw_input()
print f
frames = 'abcdefghijklmnopqrstuvwxyz'

if f > 0:
    frames = frames[:f]
    print frames

Then when I run it (my input was 2), I get this error where the variable f is not being recognized as an int, even though when I print it, my input printed an int (2).

Here's the error message: Traceback (most recent call last): File "melissa.py", line 11, in frames = frames[:f] TypeError: slice indices must be integers or None or have an index method...

So, f is 2, but I can't use it to slice the string??

paxdiablo
  • 854,327
  • 234
  • 1,573
  • 1,953

3 Answers3

1

The problem here is that you did not convert the input into an integer. Therefore, Python does not recognize it as an integer.

raw_input() will take an input from stdin and spit out a string. Python does not automatically check if the string is an integer. However, there is a method for that (isdigit()).

So what you need to do is to convert it into an integer using the int() converter before you continue:

f = int(raw_input())
Moon Cheesez
  • 2,489
  • 3
  • 24
  • 38
0

The raw_input() function returns a string regardless of what you type in.

To turn that into an actual integer, you could use something like:

f = int(raw_input())

However, be aware that is not going to go well unless you actually enter an integer. You should really catch exceptions for both a failed raw_input() and a failed int(). See here for how to do the latter, you can use a similar method to catch EOFError (and others if need be) for the former.

Community
  • 1
  • 1
paxdiablo
  • 854,327
  • 234
  • 1,573
  • 1,953
0

You have to change raw_input to input.raw_input takes it as a string whereas input takes it as a integer. Here is the code:

 print "How many frames do you want?"
    f = input()
    print f
    frames = 'abcdefghijklmnopqrstuvwxyz'

    if f > 0:
        frames = frames[:f]
        print frames
White Shadow
  • 444
  • 2
  • 10
  • 26