0

I am currently trying to make a maths quiz and I am working on trying to make the program not crash when the user doesn't answer a question(leaving the user input empty).This is what I have so far

    useranswer=raw_input("what is 8-5?")
    length=len(useranswer)

    while length<=0:
      useranswer=raw_input("please enter answer")
      length=len(useranswer)

    if int(useranswer)==3:
      print "correct"
    else:
      print"sorry wrong"

right now I'm using the method of checking the length of the useranswer using len(useranswer) to see if there is anything in the input but this only works if i use raw_input and not with a normalinput.

I wanted to know if there is any other easier way of checking if the user input is empty with a different method? I have looked for answers from a few other stackoverflow questions but none of them seemed suitable.

Thankyou!

yt.
  • 327
  • 1
  • 2
  • 11
  • 1
    `if useranswer:` # tests the truthiness of your string... empty strings are `False`. – cs95 Aug 02 '17 at 15:10
  • Why are you using `raw_input` and `input`? If you're using Python 2, you shouldn't be using `input()`, it's a potential security hole! – Chris_Rands Aug 02 '17 at 15:12
  • @cᴏʟᴅsᴘᴇᴇᴅ I don't think the dupe covers it, they're talking about using `input()` in Python 2 (for some unknown reason) – Chris_Rands Aug 02 '17 at 15:16
  • @Chris_Rands I went by the title when marking... if OP can clarify what they actually want, that'd be great. – cs95 Aug 02 '17 at 15:17
  • @cᴏʟᴅsᴘᴇᴇᴅ Agreed about the lack of clarity – Chris_Rands Aug 02 '17 at 15:18
  • @cᴏʟᴅsᴘᴇᴇᴅ Thankyou for your suggested stackoverflow page to help with my issue. I have looked through it but it doesn't quite seem like what i wanted, but thanks anyway. Also, I have edited my question and have tried to make it more specific so I hope it is ok now. – yt. Aug 02 '17 at 17:20
  • Sure, reopened. – cs95 Aug 02 '17 at 17:20
  • @cᴏʟᴅsᴘᴇᴇᴅ Thanks! – yt. Aug 02 '17 at 17:22

1 Answers1

-2

You can use

if useranswer:
    # do what you want to do here
binu.py
  • 1,137
  • 2
  • 9
  • 20
  • 2
    STOP right there. Do _not_ use `input` in python2. EVER. – cs95 Aug 02 '17 at 15:12
  • 2
    https://stackoverflow.com/questions/7709022/is-it-ever-useful-to-use-pythons-input-over-raw-input – cs95 Aug 02 '17 at 15:14
  • Basically you are saying using input can take malicious inputs from user. So you are ready to write code to cast the input from raw_input but not ready to Write some validation, that will help you do upgrade in future. instead you want to use raw_input and write all the codes all over again? – binu.py Aug 02 '17 at 15:19
  • "_instead you want to use raw_input and write all the codes all over again?_" Yes. – cs95 Aug 02 '17 at 15:20
  • Great all the best, all the best with that. – binu.py Aug 02 '17 at 15:21
  • `int(raw_input())` is a small number of bytes to pay for security – Chris_Rands Aug 02 '17 at 15:21
  • int(raw_input) will break for any input other than integer you have to put it into try and handle it, same thing you will do with input + it will solve your casting problem + it will be compatible for higher version of python – binu.py Aug 02 '17 at 15:23
  • yes, but a bit of wrapper code is a really a small price for security, best practice coding is not code golf – Chris_Rands Aug 02 '17 at 15:25
  • If you want to move to a higher version of python, use 2to3. – cs95 Aug 02 '17 at 15:32
  • @binu.py I think this answer worked and is pretty simple. Thanks – yt. Aug 04 '17 at 13:40
  • You are welcome @Ruby,you might want to take care of white-space as input depending on your need – binu.py Aug 04 '17 at 13:43