Entry
how can i get user input as a string and then make it a number? i want to use the user input to control number of interations of a loop
Feb 2nd, 2002 08:02
bbeck, Christopher Arndt, Kaz Leas,
There are multiple ways (did you expect anything else? :))
1. Use the builtin int() function
Example:
>>> s = input("Input value: ")
>>> s
'5'
>>> value = int(s)
>>> value
5
2. Use the builtin eval() function or, even easier, the builtin input()
function.
Example:
>>> value = input("Input value: ")
Input value: 5
>>>value
5
with eval():
>>>value = raw_input("Input value: ")
Input value: 42
>>> value
'42'
>>> value = eval(value)
>>> value
42