-5

What I am looking to do

So what I want to do, is get a string from an input, and convert it into an int. Here is a bit of the code so you have a picture of what I am aiming for.

age = input('How old are you: ')

for i in range(age, 18):
    print('I am', i, "years old")

I want to convert the string that I got from the input into an integer so I can use it in the i in range()... Is that possible?

General Grievance
  • 4,555
  • 31
  • 31
  • 45

2 Answers2

0

To do so you can use int(age). You may want to implement a try/catch so that the program doesn't throw an exception when the input is invalid.

D4v1d 247
  • 1
  • 1
  • 3
  • 1
    It's not a crash, it's an exception that is thrown, with traceback telling your what went wrong, and then the "program" is terminated. A crash (e.g a segmentation fault) would end the script without any details. – Countour-Integral Jan 12 '21 at 13:51
0

Hey in Python input() is by default store string type value. So you have to put

age=int(input('How old are you: '))

Here int() will store integer type value.

OR

you can also do int(age)

Ujjwal Dash
  • 767
  • 1
  • 4
  • 8