Python Forum
Beginner question: help ensuring input is a number
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Beginner question: help ensuring input is a number
#1
Hello, I am very new to programming - so please excuse the incredibly beginner question.

I have been trying to a write a couple of lines of code and am struggling to make sure that the input is a number (either int or float).

I have looked online, and have struggled to execute the suggestions of using try/except, while and .isdigit(). I am sure that it is probably a grossly easy question, but I can't seem to figure it out. Wall

I am using Python 3.6.3.

Amongst others, I have written the below:

odds_text = ("The number is ")

odds_input = input("Enter a number: ")
strip_input = odds_input.strip()

while str(strip_input): # Trying to ensure that the input was not a string.
    print("Sorry, please enter a valid number")
    break
else:
    print(odds_text.format(odds=strip_input))
Thanks for any help! Smile
Reply
#2
ck = ""
while type(ck) == str:
    odds_text = ("The number is ")
    odds_input = input("Enter a number: ")
    strip_input = odds_input.strip()
    try:
        ck = float(strip_input)
    except:
        print("Sorry, please enter a valid number") ; ck = ""
Reply
#3
Doing unnecessary stuff there ezdev.
No need to check type,and is same code use try/except(should not be bare).

Catch the error(ValueError) if input need a integer,and give a message to try again.
If use function can use return,the no need to break out.
Function will only return a number, int(input(' ')) or float(input(' '))
def foo():
    while True:
        try:
            odds_input = int(input('Enter a number: '))
            return odds_input
        except ValueError:
            print('Sorry please enter a valid number,try again')

print(foo())
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Very Beginner question on simple variables Harvy 1 158 Apr-12-2024, 12:03 AM
Last Post: deanhystad
  A simple "If...Else" question from a beginner Serena2022 6 1,681 Jul-11-2022, 05:59 AM
Last Post: Serena2022
Question Beginner Boolean question [Guessing game] TKB 4 2,280 Mar-22-2022, 05:34 PM
Last Post: deanhystad
  input function question barryjo 12 2,701 Jan-18-2022, 12:11 AM
Last Post: barryjo
Big Grin General programming question (input string)[ jamie_01 2 1,587 Jan-08-2022, 12:59 AM
Last Post: BashBedlam
Exclamation question about input, while loop, then print jamie_01 5 2,644 Sep-30-2021, 12:46 PM
Last Post: Underscore
  Beginner question NameError amazing_python 6 2,429 Aug-13-2021, 07:28 AM
Last Post: amazing_python
  Beginner question - storing values cybertron2 4 3,174 Mar-09-2021, 04:21 AM
Last Post: deanhystad
  beginner question about lists and functions sudonym3 5 2,708 Oct-17-2020, 12:31 AM
Last Post: perfringo
  an input question saratha 4 2,058 Jul-15-2020, 04:01 AM
Last Post: ndc85430

Forum Jump:

User Panel Messages

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