I’m new to python. I wanted to know if when I use the input command, is it common to write the type of variable we expect to recieve from the input?
- num = int(input())
The other option: just
What is more conventional and used?
Hi and welcome.
Each to their own; myself, I tend to use the default return of the
input() function, which is a string object. The reason being is that if (to use your example) you code
int(input()) then you then have to rely on a user not to enter anything that can’t be type converted to a integer value. There are (of course) ways to prevent a script crash in the event that a user does not play nice and in any case you’re going to want to do a check on what has been entered. That process is called ‘sanitization’ and you’ll maybe come across the mantra: sanitize your users input.
See: Top 10 Best Coding Practices
But, like said; each to their own.
wow! Thank you very much! Very helpful
Great to have people responding like this!