I am using Python 3.1.2
What is the best way to get numeric (float or int) user input?
HiHe
174
Junior Poster
Recommended Answers
Jump to PostHere is a function that will check for valid numeric input ...
# a simple input function to guarantee numeric input # tested with Python 3.1.2 def input_num(prompt="Enter a number: "): """ prompt the user for a numeric input prompt again if the input is not numeric …
Jump to PostHere some first version of number testing without support yet for numbers in 1e7 scientific format.
This is unfortunately Python 2.6 code, probably runs also in python 3, not tested.
Routine strips whitespace and zeroes before numbers and puts '0' for whole part for numbers like .23423 -> …
Jump to PostHow about this.
#Python 3 try: input_num = float(input('test: ')) except ValueError: print ('Not an integer') print (input_num)
All 9 Replies
jcao219
18
Posting Pro in Training
bumsfeld
413
Nearly a Posting Virtuoso
vegaseat
1,735
DaniWeb's Hypocrite
Team Colleague
arithehun
commented:
Creative
+0
TrustyTony
888
pyMod
Team Colleague
Featured Poster
vegaseat
1,735
DaniWeb's Hypocrite
Team Colleague
TrustyTony
888
pyMod
Team Colleague
Featured Poster
TrustyTony
888
pyMod
Team Colleague
Featured Poster
vegaseat
commented:
nice
+13
snippsat
661
Master Poster
TrustyTony
888
pyMod
Team Colleague
Featured Poster
Be a part of the DaniWeb community
We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.