python - convert string to int? -


working on letter-guessing game.

why in following example, when hardcode value of variable, "userguessposition" 2, code works expected.

secretword = ('music') userguessposition = 2  slice1 = (secretword.__len__()) - userguessposition - 1   print (secretword[slice1:userguessposition]) 

but when rely on input() function , type in 2 @ prompt, nothing happens?

secretword = ('music') userguessposition = 0 userguessposition == input() slice1 = (secretword.__len__()) - userguessposition - 1   print (secretword[slice1:userguessposition]) 

i assume because keyboard input of "2" being seen string , not integer. if case, i'm unclear on proper syntax convert it.

the problem not input recognized string, rather in syntax: you're doing comparison operation should doing assignment operation.

you have use

userguessposition = input() 

instead of

userguessposition == input() 

the input() function convert input number appropriate type, sp should not issue. if need convert string (say, my_string) integer, need my_int = int(my_string).

edit

as mentioned below @henrykeiter, depending on python version, may in fact need convert return value of input() integer hand, since raw_input() (which takes in input string) renamed input() in python 3.


Comments

Popular posts from this blog

monitor web browser programmatically in Android? -

Shrink a YouTube video to responsive width -

wpf - PdfWriter.GetInstance throws System.NullReferenceException -