Louis Foster Louis Foster - 1 month ago 7
Python Question

Why do I get this TypeError?

I believe this error means I can't include a variable in a loop however I am struggling to see a way around....

the error is

TypeError: range() integer end argument expected, got unicode.

The problem the book tried to ask me is:

Try wring a program the will prompt for an number and print the correct times table (up to 12).

This is my code:

def main():
choice = raw_input("Which times table would you like")
print ("This is the", choice , "'s times table to 12")

var1 = choice*12 + 1

for loopCounter in range (0,var1,choice):

if __name__ == '__main__':

Any suggestions? thanks in advance.


The raw_input function gives you a string, not an integer. If you want it as an integer (such as if you want to multiply it by twelve or use it in that range call), you need something such as:

choice = int(raw_input("Which times table would you like"))

There are potential issues with this simplistic solution (e.g., what happens when what you enter is not a number), but this should be enough to get past your current problem.