1

Backstory: I have been trying to actually learn python instead of just snipping from others. I have created a simple script that uses webbrowser. It may be a dirty script and I would love input like "you should do this", "this can be simplified". The thing i cant figure out is using if statement to handle incorrect input, prompt, then recheck the if statement. I tried searching but nothing assisted in this.

import webbrowser
a = input ('Do you want to search?(y or n)')
if a == ('y' or 'yes' or 'Y' or 'Yes' or 'YES'):
    b = input ('What do you want to search?')
    ab = ('https://www.google.com//search?q='+b)
    urlab = ab
    webbrowser.open(urlab)

else:
    x = input('Where do you want to go?: ')
    new = 2 # open in a new tab, if possible
    # open a public URL, in this case, the webbrowser docs
    url = x
    webbrowser.open(url)

The question is: How do i ether do a recurring that will handle incorrect answers. If they use something other then the listed yes, it will print please use ('y' or 'yes' or 'Y' or 'Yes' or 'YES'), then prompt again and allow for input. I know i will have to change it to a nested if statement to allow the same with no to move to next. Also as is, when i use the code and enter 'y' it will open with my default (firefox), but if i enter anything else, it only opens in IE without the google search but "searching" like http://fun/ instead of https://www.google.com//search?q=fun as it should.
What did leave out? Also if you could post information on in-depth the meaning behind the code to help further learning. Thank you all!

3
  • This is off the subject of your question but make yourself a promise this is the last time you use rubbish variable names like 'a', 'b', 'x'. Everyone who has to read code like that hates the person who wrote it and that will include you when you come back to read your own code in a few months time. Use meaningful names instead: 'a' might be 'continue'; 'b' might be 'search_string'; 'x' might be 'new_url'. Make yourself this promise and save hating yourself later :). Commented Sep 18, 2016 at 7:31
  • thank you for the constructive. This was they way i was shown in my basic class and it kinda rubbed off. I'll keep that in mind and definitely will do that! Thanks! Commented Sep 18, 2016 at 7:36
  • i have a shocking head cold and just re-read my comment in a slightly better state of mind and just wanted to point out before someone else does that my suggestion of using 'continue' as a variable name is a very stupid one. 'continue' is a reserved. Maybe 'user_continue' or something similar would make more sense. Sorry, but I hope the main point made some sense in spite of my blunder :) Commented Sep 18, 2016 at 11:17

2 Answers 2

1

The following code should work:

import webbrowser
a = 'y'
while a != 'n':
    a = input ('Do you want to search?(y or n)')
    a = a[0].lower()
    if a in  "y":
        b = input ('What do you want to search?')
        ab = ('https://www.google.com//search?q='+b)
        urlab = ab
        webbrowser.open(urlab)

    elif a not in "n":
        print "Please only enter 'y' or 'n'."

The while loop tells python to loop as long as the answer is not "n".

The a = a[0] tells python to only use the first letter of the response. This is to make the comparison easier later on.

The .lower() code tells python to convert the result to lowercase. Again, this is to make the comparison easier later on.

Now our answer will always be lowercase, and the first letter entered. So y ,yes, Yes, YES and n, no, No, NO will be converted to y or n. Of course, any other text will be treated the same way (lowercase and first character), but we only care about y and n.

The rest should be pretty straightforward. Since we have limited the possibilities of what the answer can be, we can do a simple comparison.

Below are modifications to check for only yes and no:

import webbrowser
a = 'y'
while a != 'n':
    a = input ('Do you want to search?(y or n)')
    a = a.lower()
    if a in  ("y", "yes"):
        b = input ('What do you want to search?')
        ab = ('https://www.google.com//search?q='+b)
        urlab = ab
        webbrowser.open(urlab)
    elif a in "no"
        a = "n"
    elif a not in ("n", "no"):
        print "Please only enter 'y' or 'n'."
Sign up to request clarification or add additional context in comments.

3 Comments

@Leva7's comparisons are better than mine because mine will treat anything starting with "y" as yes, and anything starting with "n" as no. In this instance, though, it may not be an issue.
Thank you bud, i really like the converting. Something i can use now and later on when i get even deeper. Thanks again!
Don't forget to select an answer then ;)
1

There's a different way to check if the value is one of the given values. You should make your if condition be like this:

if a in ('y', 'yes', 'Y', 'Yes', 'YES'):

It is fast and understandable.

Here's a reason why your condition does not work.
Let's say we entered 'no'. Let's see what happens in your if statement:
1. First, the ('y' or 'yes' or 'Y' or 'Yes' or 'YES') is evaluated. Since a non-empty string in Python converts to True, this part evaluates entirely to True
2. Then, the comparison takes place. It looks like this:

if a == True:

With a being a string 'no'. That's obviously not what you want. Use the method a described above and everything will be fine

To constantly re-ask until a correct input is received, try an endless loop with a break statement:

while True:
    a = input()
    if a in ...:
        # process input
        break
    else:
        print("please use 'y' or 'yes' or 'Y' or 'Yes' or 'YES'")

1 Comment

As always the python community delivers with clear and non trollish responses. Thank you very much for this. I may look at using both yours and @jasonD replies just to learn how to use both styles. Thanks again!!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.