The question is to keep asking a person to say their favorite websites and stops after they type done. We want to extract the "www." and ".com" and appends that part to a list and finally print the list. I can't figure out what im doing wrong.
myweb=[]
while True:
mywebsite=input("what is your favorite website?")
if mywebsite=="done":
print(myweb)
break
else:
myweb.append(mywebsite)
continue
mywebsite=mywebsite.split('www.')
mywebsite=mywebsite.split('.com')
print(mywebsite)
based on your question, i make this function
myweb=[]
while True:
mywebsite=input("what is your favorite website?")
if mywebsite=="done":
print(myweb)
break
else:
myweb.append(mywebsite)
continue
# allocating enough space
mywebsite = [0]*len(myweb)
# extract it
for i in range(len(myweb)):
# split based on www.domain.com
mywebsite[i] = myweb[i].split('.')[1]
print(mywebsite)
Use regex on each item in your list
>>> import re
>>> website = 'www.amazon.com'
>>> website = re.search('www\.(.*)\.com',website).group(1)
>>> website
'amazon'
if all the input 's pattern is www.SOME_WEB_SITES.com, so you can use
websites = [ '.'.join(x.split('.')[1:-1]) for x in a]
Think about the subdomain cases too, for example:
www.a.com --> a
www.abcom --> ab
x.split('.')[1:-1] # we will get an array ["a","b"]
'.'.join(....) # we need to join them by .
You use a python strings .strip()
function to do this.
myweb=[]
while True:
mywebsite=input("what is your favorite website?")
if mywebsite=="done":
for x in myweb:
print(x.strip("www." + ".com")) #Strips www. and .com from the string.
break
else:
myweb.append(mywebsite)
This can be easily done using regular expressions. However it looks like you are new to python. So the following corrected code might help you out.
myweb_name=[]
while True:
mywebsite=input("what is your favorite website?")
if mywebsite=="done":
break
else:
myweb_name.append(mywebsite.split('www.')[1] # remove the www part
.split('.com')[0]) # remove the .com part
continue
print(myweb_name)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.