0

I was wondering how you could make it so that input accepts numbers and letters.

I tried

int(input("blah blah blah")

but that doesn't work for letters and it accepts only numbers.

3 Answers 3

2

You can use exceptions to attempt the int conversion, and otherwise treat the input as a string:

my_input = input("blah blah blah")
try:
    int_input = int(my_input)
except ValueError:
    do_something_with_a_string(my_input)
else:
    do_something_with_an_int(int_input)
Sign up to request clarification or add additional context in comments.

Comments

0

When you put everything within an int() it forces whatever the input is to be an integer. That is why you don't seem to getting any letters. Try removing the int()

for python 3.x

inputFromUser = input("blah blah blah")

or

for python 2.x

inputFromUser = raw_input("blah blah blah")

3 Comments

Thanks it worked! Also, what is the difference between raw_input and input if you don't mind me asking?
raw_input() has been renamed to input() in Python 3.x
and input() in 2.x is the same as eval(raw_input())
0

another problem with that is that you havnt put two brackets at the end of int stuf....

int(input("blah blah blah"))

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.