Bottom Page

Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
 Beginner question: help ensuring input is a number
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")
Thanks for any help! Smile
ck = ""
while type(ck) == str:
    odds_text = ("The number is ")
    odds_input = input("Enter a number: ")
    strip_input = odds_input.strip()
        ck = float(strip_input)
        print("Sorry, please enter a valid number") ; ck = ""
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:
            odds_input = int(input('Enter a number: '))
            return odds_input
        except ValueError:
            print('Sorry please enter a valid number,try again')


Top Page

Possibly Related Threads...
Thread Author Replies Views Last Post
  Beginner question: lxml's findall in an xml namespace aecklers 0 119 Jan-22-2020, 10:53 AM
Last Post: aecklers
  Super easy beginner question AkulaLA 3 205 Nov-07-2019, 03:42 AM
Last Post: Larz60+
  Find Average of User Input Defined number of Scores DustinKlent 1 204 Oct-25-2019, 12:40 AM
Last Post: Larz60+
  Basic Beginner question NHeav 4 314 Sep-13-2019, 11:43 AM
Last Post: NHeav
  Beginner Question - Esaping the Escape Character correctly? Bramen 4 408 Aug-27-2019, 02:38 PM
Last Post: Bramen
  A question about subprocess taking input from command line and returning output! Aurimas 8 832 May-15-2019, 04:02 PM
Last Post: Aurimas
  question by a beginner salahoum 11 1,103 Jan-28-2019, 12:02 PM
Last Post: perfringo
  beginner question Naito 2 470 Jan-15-2019, 07:59 AM
Last Post: Naito
  "if" beginner question Naito 13 1,414 Jan-13-2019, 02:15 PM
Last Post: perfringo
  Absolute Beginner Python Question matchamochi7 5 975 Nov-02-2018, 12:09 PM
Last Post: wookie

Forum Jump:

Users browsing this thread: 1 Guest(s)