samedi 17 septembre 2016

If statement with input check with incorrect outputs. -Python 3.4

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 dirty script and would love input for "you should do" "can be simplified". The thing i cant figure out is using If statement to handle incorrect, 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 = ('http://ift.tt/vgrAeu'+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 nestled if statement to allow the same with no to move to next. Also as is, when i use the code and use 'y' it will open with my default (firefox) if i use any other it only opens in IE without the google search but "searching" like http://fun/ instead of http://ift.tt/2cJ6DBL 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!

Aucun commentaire:

Enregistrer un commentaire