Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Improvements?
#1
the Task asks me to Develop a program that analyses a sentence that contains several words without punctuation. When a word in that sentence is input, the program identifies all of the position the word occurs in the sentence. The system should not be case sensitive: Ask, ask & ASK should be treated as the same word.
 
I have completed it and my code is as follows 
import re

#imports regular expression

sentence = str(input("Enter a sentence without punctuation: ")).lower()

# this creates a variable that only allows words, the user inputs a sentence.

#.lower() makes the senctence case insensitive 

valid = re.match("^[a-z’ ‘]*$" , sentence)

#This checks that there is no punctuation in the sentence the user inputs

while not valid:

    print("The sentence you have entered contains punctuation.")

    sentence = input("Enter a sentence without punctuation: ").lower()

    valid = re.match("^[a-z’ ‘]*$" , sentence)

#if the user enters a sentence which contains punctuation the user is told to re-enter the senctence as it is invalid

#this process repeats until the user enters a valid sentence, as we are using a while loop

print("You have entered a sentence without punctuation.")

#This is printed once the user has correctly entered a sentence

keyword = str(input("input a keyword from the sentence: ")).lower()

# this creates a variable that only allows words, the user inputs a keyword from the sentence.

#.lower() makes the keyword case insensitive

words = sentence.split(' ')

#this will split the variable sentence in to a string array using the separator defined.. If no separator is defined, whitespace is used

while keyword not in sentence:

    print("The selected word of choice that you have entered is not in the sentence!")

    keyword = input("Enter a keyword from the sentence: ").lower()

    words = sentence.split(' ')

#The while loop allows us to repeatedly ask the user to enter their sentence until it is valid

print("The key word you selected has been found!")

#the notifies the user that the keyword has been found

positions = [i + 1 for i, w in enumerate(words) if w == keyword]

#this finds the position of the word in the sebtence 

print("The word " + "'" + keyword + "'" + " is in the position(s):")

#this line of code tells the user that their keyword has been found and is in the position...

print(positions)

#this prints out the position of the desired word in the sentence, finishing the above line.
My question is are there any kind of improvements that can be made eg to make the code more efficient? Any help will be appreciared.
Reply
#2
Yes, but if it's for homework you should probably just leave it as-is. Otherwise it'll look like you just copied code from the internet :p

That said, you can remove line 47 entirely. You already set "words" before the while loop, so setting it repeatedly within the while loop is not necessary. It doesn't hurt anything, it's just not needed.
Reply
#3
Assuming python3:
1. No need to use str on lines 5 and 31 to convert user input. it's already str. Look at how you do it on lines 19 and 45
2 enumerate has additional argument start. you can use it on line 55. use start=1 instead of i+1
3. on line 41 - while keyword not in sentence, what shall happen if keyword str is just part of another str/word?
4. lines 59 and 63 - str formatting and join?
Reply
#4
You also do not need to specify 'str' in conjunction with the input statement, as 'input' by default, will create a string.

so this:
sentence = str(input("Enter a sentence without punctuation: ")).lower()
is the same as:
sentence = input("Enter a sentence without punctuation: ").lower()
You only need to prepend when you want something other than a string. For example, to expect an integer from the user:
answer = int(input("Enter an integer: ")
If it ain't broke, I just haven't gotten to it yet.
OS: Windows 10, openSuse 42.3, freeBSD 11, Raspian "Stretch"
Python 3.6.5, IDE: PyCharm 2018 Community Edition
Reply


Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020