Identifying no.s as strings

the code is identifying the numbers I enter as string
1

2

The function input gives a string. You have to do the interpretation. In in this case, interpret it as an int.

imp = int(input('input a no'))

You could check for problems, in case they input something that cannot be interpreted as an int.

try:
  imp = int(input('input a no'))
except ValueError:
  ... # Respond to the error. For example, tell the user that they made a mistake.

thanks. it worked
when i open the code using python, when i press “enter” after entering the no., the software just stops. any idea why that is happening?

Did you make sure the window has focus while typing? It doesn’t look like the program is receiving your input at all, so it’s still waiting for you to “enter the number”.

Focus while typing? I don’t understand.
If it’s about the first one, it was solved, thanks to @franklinvp. If it’s about second, when I enter the number and click enter, python software is just closing. Like how it does when you click the X

Oh, sorry. When you said “the software just stops” I thought you meant that the window stays open and you don’t see anything change inside it, like in the screenshot. That would happen if you were typing in a different program instead of that one.

The window closes because there’s nothing more for your program to do, so Windows doesn’t have a reason to keep the window open any more.

I was opening this code
try:
inp = int(input(‘enter a no’))
except:
print(‘please enter a no.’)
quit()
inp = inp + 2.5
print(‘your no is’, inp)

using python. image.
The code runs on terminal just fine. But when opened using python, the window closes as soon as I enter the number and click enter. It is not giving any output while there should be.

Exactly as I said. The output is there, you just don’t get to see it because the window closed.

How do I prevent it from closing?

By checking the links I posted already and following the advice given there.

1 Like