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
#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


Messages In This Thread
RE: Beginner question: help ensuring input is a number - by snippsat - Dec-23-2017, 10:49 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Very Beginner question on simple variables Harvy 1 167 Apr-12-2024, 12:03 AM
Last Post: deanhystad
  A simple "If...Else" question from a beginner Serena2022 6 1,700 Jul-11-2022, 05:59 AM
Last Post: Serena2022
Question Beginner Boolean question [Guessing game] TKB 4 2,294 Mar-22-2022, 05:34 PM
Last Post: deanhystad
  input function question barryjo 12 2,705 Jan-18-2022, 12:11 AM
Last Post: barryjo
Big Grin General programming question (input string)[ jamie_01 2 1,591 Jan-08-2022, 12:59 AM
Last Post: BashBedlam
Exclamation question about input, while loop, then print jamie_01 5 2,649 Sep-30-2021, 12:46 PM
Last Post: Underscore
  Beginner question NameError amazing_python 6 2,442 Aug-13-2021, 07:28 AM
Last Post: amazing_python
  Beginner question - storing values cybertron2 4 3,194 Mar-09-2021, 04:21 AM
Last Post: deanhystad
  beginner question about lists and functions sudonym3 5 2,721 Oct-17-2020, 12:31 AM
Last Post: perfringo
  an input question saratha 4 2,066 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