How do I check if the input is numbers or alphabets?

I went through many documents and it states to use string.isalpha

I am using pycharm and I do not see this option.

I tried this:

if user_input.isdigit():
user_input_number = int(user_input)
calculated_value = days_to_hours(user_input_number)
print(calculated_value)
else:
print(‘This is not a valid number’)

but i get error:

Enter the number of days: 2
Traceback (most recent call last):
File “C:\Users\Admin\pythonProject1\main.py”, line 16, in
if user_input.isdigit():
AttributeError: ‘int’ object has no attribute ‘isdigit’

Process finished with exit code 1

You can view the relevant string methods in the docs-
https://docs.python.org/3/library/stdtypes.html#string-methods. These are string methods though, in your example you are using an integer type which would not have the same methods.

1 Like