Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 2 of 2
  1. #1
    New Coder
    Join Date
    Oct 2010
    Posts
    40
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Test if input is number or string

    Hello CF

    Im new to python, and Im struggling with a code here.

    How do i test if the user input is a number or a string?

    print("Hello")

    i = raw_input("Type a number = ")

    if not a number:
    print("Illegal")

    The ( If not a number ) is where i need my code..

    Best Regards,

    Nap

  • #2
    Regular Coder
    Join Date
    Mar 2006
    Location
    Brasília, Brazil
    Posts
    153
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Code:
    i = raw_input("Type a number = ")
    try:
       i = float(i)
    except ValueError:
       print("Illegal")


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •