i trying write script takes word , prints first 3 characters, last 3 characters, , whatever lies in middle in dots:
abracabra
abr...bra
i made work,
word = input("what's word ?") first = str(word[0:3]) last = str(word[-3:]) middle = int(len(word)-6) middots = "." * (middle) print((first)+(middots)+(last))
but on 1 line, can in bash, example returns list of network interfaces:
intfaces=$(/sbin/ifconfig -a | sed 's/[ \t].*//;/^\(lo\|\)$/d')
how do python? tried this, did not work:
word = input("what's word ?") middots = "." * (int(len(word)-6)) print(str(word[0:3])+(middots)+ str(word[-3:]))
what correct syntax?
edit
thanks helping me not correct, understand well. here ended going with...
def print_dotted_word(): word = str(input("what's word ?")) if len(word)<7: raise valueerror("needs @ least 7 letters.") print(word[:3] + '.'*(len(word)-6) + word[-3:]) while true: try: print_dotted_word() break except valueerror:("needs @ least 7 letters.")
you can following:
word = input("what's word ?") if len(word)<7: raise valueerror("please enter word greater 6 characters") print(word[:3] + '.'*(len(word)-6) + word[-3:])
here, raise valueerror
exception if word
entered less 7 characters.
we can check in python shell enclosing code in function print_dotted_word()
.
python 2.7:
in [1]: def print_dotted_word(): word = raw_input("what's word ? \n") # use raw_input if len(word)<7: # check word length raise valueerror("please enter word greater 6 characters") # raise exception print word[:3] + '.'*(len(word)-6) + word[-3:] # print desired response in [2]: print_dotted_word() what's word ? helloworld hel....rld in [3]: print_dotted_word() what's word ? hello --------------------------------------------------------------------------- valueerror traceback (most recent call last) ----> 1 print_dotted_word() 2 word = raw_input("what's word ? \n") 3 if len(word)<7: ----> 4 raise valueerror("please enter word greater 6 characters") 5 print word[:3] + '.'*(len(word)-6) + word[-3:] valueerror: please enter word greater 6 characters
python 3.4:
in [1]: def print_dotted_word(): word = input("what's word ? \n") # use input here if len(word)<7: # check word length raise valueerror("please enter word greater 6 characters") # raise exception print(word[:3] + '.'*(len(word)-6) + word[-3:]) # print desired response in [2]: print_dotted_word() what's word ? helloworld hel....rld in [3]: print_dotted_word() what's word ? hello --------------------------------------------------------------------------- valueerror traceback (most recent call last) ----> 1 print_dotted_word() 2 word = input("what's word ? \n") 3 if len(word)<7: ----> 4 raise valueerror("please enter word greater 6 characters") 5 print(word[:3] + '.'*(len(word)-6) + word[-3:]) 6 valueerror: please enter word greater 6 characters
Comments
Post a Comment