Welcome guest. Before posting on our computer help forum, you must register. Click here it's easy and free.

Author Topic: python help  (Read 2703 times)

0 Members and 1 Guest are viewing this topic.

blayze101

  • Guest
python help
« on: August 19, 2011, 12:40:07 AM »
Hello.
I've got a simple problem i can't find a solution to, although i know it's something very simple.

Okay how can i return an error message when having the user input something, but they dont enter anything and just press enter, i'm thinking along the lines of like:
Code: [Select]
x = input("Type something: ")
if x == null:
print("you didn't enter anything")

But yeah i need to know what you would type to signify like nothing/no data. Or perhaps there is a way to do it via except statements.

Could anyone shed some light on this for me?

Sidewinder



    Guru

    Thanked: 139
  • Experience: Familiar
  • OS: Windows 10
Re: python help
« Reply #1 on: August 19, 2011, 06:37:33 AM »
Hmmm. Last week it was Java, this week it's Python.  I have risen to my level of incompetence. ;D

Google helped a lot here since my Python skills are less than zero.

Code: [Select]
x = raw_input("Enter something: ")
if x == "":
  print "You entered nothing"
else:
  print "You entered:", x;

Runs under Python 2.6.1.1 if that makes a difference.

Good luck.  8)
The true sign of intelligence is not knowledge but imagination.

-- Albert Einstein

ghostdog74



    Specialist

    Thanked: 27
    Re: python help
    « Reply #2 on: August 27, 2011, 05:30:09 AM »
    Hmmm. Last week it was Java, this week it's Python.  I have risen to my level of incompetence. ;D

    Google helped a lot here since my Python skills are less than zero.

    Code: [Select]
    x = raw_input("Enter something: ")
    if x == "":
      print "You entered nothing"
    else:
      print "You entered:", x;

    Runs under Python 2.6.1.1 if that makes a difference.

    Good luck.  8)


    x=="" is one way... but here's a more "Pythonic" one , whatever that means
    Code: [Select]
    ....
    if not x:
      ....