Python Forum
Get input directly as a number?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Get input directly as a number?
#1
In my little, amateurish Python routines, I often need to get a number as input, so I do:

Quote:num = input('just enter a number ')
startNum = int(num)

Today, I just thought, "Is it possible to get a number directly?"

Is it?
Reply
#2
https://stackoverflow.com/questions/2044...as-numbers
Reply
#3
yes it is possible use the int function
example - int(input('enter a number '))
this way you can take a number directly as a input
Reply
#4
Please use proper code tags while coding. You can do :
num = int(input("Just enter a number : "))
pyzyx3qwerty
"The greatest glory in living lies not in never falling, but in rising every time we fall." - Nelson Mandela
Need help on the forum? Visit help @ python forum
For learning more and more about python, visit Python docs
Reply
#5
In python 2.7 and before the conversion was done automatically. In effect the old "input" was eval(raw_input(prompt)). So if I typed 1.23 input would return a float, enter 5 and you get an int, and enter abc you get something else.
var var = 'a variable'
while True:
    raw = input('Enter value ')
    cooked = eval(raw)
    print(raw, cooked, type(cooked))
A problem with this is you have to enter something that can be evaluated.
Output:
Enter value 1 1 1 <class 'int'> Enter value 2 2 2 <class 'int'> Enter value var var a variable <class 'str'> Enter value abc Traceback (most recent call last): File "C:\Users\djhys\Documents\Python\Musings\junk.py", line 5, in <module> cooked = eval(raw) File "<string>", line 1, in <module> NameError: name 'abc' is not defined
Python 3 gets around this problem by always returning a string and forcing us to do any required conversions. A far better approach in my opinion. Input is a very personal thing.

I hardly ever use input, but looking at all the posts in the forum I know I am a minority. If you use input a lot I think it worth spending some time and making a nice package of input functions. You could have input_int, input_float, input_number, input_imaginary, input_choice. You could have a default value that is returned if the entered input is not the right type. You could have a flag that keeps asking for input until a valid input is received. There are so many things that could be done once and then reused over and over instead of making the same mistakes over and over and over.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Import XML file directly into Excel spreadsheet demdej 0 836 Jan-24-2023, 02:48 PM
Last Post: demdej
  How to load log.txt directly into python codes? sparkt 6 2,964 Aug-21-2020, 03:51 PM
Last Post: sparkt
  if the input is not number, let user input again teatea 14 8,529 Jun-02-2020, 07:03 PM
Last Post: ibutun
  TypeError indexing a range of elements directly on the list JFerreira 2 2,199 Mar-30-2020, 04:22 PM
Last Post: bowlofred
  Find Average of User Input Defined number of Scores DustinKlent 1 4,279 Oct-25-2019, 12:40 AM
Last Post: Larz60+
  How to modify __init__ of built-in module directly from the script? sonicblind 5 3,545 Jul-31-2018, 12:48 PM
Last Post: sonicblind
  Why do we use __main__ when we can call a function directly? Dave7Swift 5 3,840 Jun-04-2018, 05:01 AM
Last Post: Dave7Swift
  How can i restrict the number of characters in an input? kevencript 1 4,034 May-23-2018, 05:14 AM
Last Post: micseydel
  Guess Random Number Why i m not able to enter input Nithya Thiyagarajan 6 8,183 Jan-07-2018, 04:26 AM
Last Post: squenson
  Beginner question: help ensuring input is a number ycrad 2 2,894 Dec-23-2017, 10:49 PM
Last Post: snippsat

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020