Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
input check help!
#4
Whenever you input something like

x = input()
Then the variable (x in this case) is always a string. In some of your other examples you're asking if it's a float, or an instance of a float, etc. It will never be. The contents might look like an integer, but the variable remains a string. You can attempt to cast that value it to a int or something else:

x = input()
i = int(x)
But of course that can fail if the input text has characters in it. What your first one does is try to cast it this way, and print a message if that fails.

x = input()
try:
    i = int(x)
    [do stuff here with your int...]
except ValueError:
    # Ooops, something in the try section failed with a valueerror.  That likely
    # means the string didn't look like an int.  We can complain here, and maybe
    # loop to retry.
    print(f"The input {x} doesn't seem to be a number. I don't know what to do with it.")
Reply


Messages In This Thread
input check help! - by bntayfur - Jun-02-2020, 08:06 PM
RE: input check help! - by bowlofred - Jun-02-2020, 08:51 PM
RE: input check help! - by bntayfur - Jun-02-2020, 09:27 PM
RE: input check help! - by bowlofred - Jun-02-2020, 11:38 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Help with to check an Input list data with a data read from an external source sacharyya 3 1,908 Mar-09-2024, 12:33 PM
Last Post: Pedroski55
  I want to check if the input is str or is int & if it's str repeat the loop HLD202 4 4,178 Nov-23-2020, 11:01 PM
Last Post: perfringo
  Check all input, easy way! Help pls! bntayfur 2 2,511 Jul-05-2020, 10:58 PM
Last Post: bntayfur
  how i can check the input type? Firdaos 3 3,815 Dec-13-2018, 11:39 PM
Last Post: wavic
  Validating Input (basic check for int etc) gruntfutuk 1 3,251 Aug-06-2018, 07:43 AM
Last Post: gruntfutuk
  check if input is correct before proceeding Pedroski55 1 3,765 Sep-16-2017, 01:56 AM
Last Post: rdx

Forum Jump:

User Panel Messages

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