Ask coding questions

← Back to all posts
str input to int()
crazyCoder691 (8)

I have this list call it mylist

mylist = ["something", "something else", "bob"]

I ask the user for input:

print ("Hi! Give a number")
number = input()

and have it do this:

print (mylist[number])

It gives me an error that says that it needs to be an integer to be able to use it with a list. When I get input from the user, it makes it str() automatically. Does anyone know how to change the input() variable to an int() or automatically make it an int()?

Commentshotnewtop
Giothecoder (130)

int(input(“enter a number”))

That should work..

crazyCoder691 (8)

So, @Giothecoder do it like this:

number = int(input("Give a Number ")