Post: Python Task Help Please
07-14-2016, 01:50 PM #1
Ali
Can’t trickshot me!
(adsbygoogle = window.adsbygoogle || []).push({}); Hey, As per a school assignment ive been given a task which brings me difficulties.

Task:
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 positions where the word occurs in
the sentence. The system should not be case sensitive: Ask, ask, ASK should be treated as the same
word.

Ive messed around a lot with it but I only have this right now:
    
sentence = input("Enter a sentence")
print(sentence)
sentence.split()
split = sentence.split()
word = input("Enter a word from your sentence...")
if word in split:
***** print("The word: ",word," is located on",split(pos))
else:
***** print("Error! Word not found in your sentence.")


The ***** is indentation. Idk why it displays asterisk but oh well...

I know theres obvious mistakes like split(pos) and no breaks etc. I'm also supposed to make it on how only characters can be submitted so I need code to say if sentence contains punctuation or numbers...

I haven't made my if else if else statements for the numbers and punctuation but that's no issue really. It's just the splitting and making it display the word position of the word submitted.
07-14-2016, 02:35 PM #2
Toxic
former staff
Originally posted by Ali View Post
Hey, As per a school assignment ive been given a task which brings me difficulties.

Task:
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 positions where the word occurs in
the sentence. The system should not be case sensitive: Ask, ask, ASK should be treated as the same
word.

Ive messed around a lot with it but I only have this right now:
    
sentence = input("Enter a sentence")
print(sentence)
sentence.split()
split = sentence.split()
word = input("Enter a word from your sentence...")
if word in split:
***** print("The word: ",word," is located on",split(pos))
else:
***** print("Error! Word not found in your sentence.")


The ***** is indentation. Idk why it displays asterisk but oh well...

I know theres obvious mistakes like split(pos) and no breaks etc. I'm also supposed to make it on how only characters can be submitted so I need code to say if sentence contains punctuation or numbers...

I haven't made my if else if else statements for the numbers and punctuation but that's no issue really. It's just the splitting and making it display the word position of the word submitted.


Get on skype asap
07-14-2016, 03:07 PM #3
Originally posted by Ali View Post
snip


    
def isalphaspace(string):
return all(x.isalpha() or x.isspace() for x in string);

sentence = input("Enter a sentence: ")
word = input("Enter a word from your sentence: ")
if not isalphaspace(sentence) or not isalphaspace(word):
print("Input can only be letters");
quit();
if word in sentence:
print("The word: ", word, " is located on", sentence.lower().find(word.lower()))
else:
print("Error! Word not found in your sentence.")
Last edited by John ; 07-14-2016 at 03:15 PM.
07-15-2016, 03:32 AM #4
Toxic
former staff
Originally posted by Ali View Post
Hey, As per a school assignment ive been given a task which brings me difficulties.

Task:
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 positions where the word occurs in
the sentence. The system should not be case sensitive: Ask, ask, ASK should be treated as the same
word.

Ive messed around a lot with it but I only have this right now:
    
sentence = input("Enter a sentence")
print(sentence)
sentence.split()
split = sentence.split()
word = input("Enter a word from your sentence...")
if word in split:
***** print("The word: ",word," is located on",split(pos))
else:
***** print("Error! Word not found in your sentence.")


The ***** is indentation. Idk why it displays asterisk but oh well...

I know theres obvious mistakes like split(pos) and no breaks etc. I'm also supposed to make it on how only characters can be submitted so I need code to say if sentence contains punctuation or numbers...

I haven't made my if else if else statements for the numbers and punctuation but that's no issue really. It's just the splitting and making it display the word position of the word submitted.

Seeing john has answered your question, im gonna close the thread Sal
If you want it to be re-opened, PM me Winky Winky
Originally posted by John View Post
    
def isalphaspace(string):
return all(x.isalpha() or x.isspace() for x in string);

sentence = input("Enter a sentence: ")
word = input("Enter a word from your sentence: ")
if not isalphaspace(sentence) or not isalphaspace(word):
print("Input can only be letters");
quit();
if word in sentence:
print("The word: ", word, " is located on", sentence.lower().find(word.lower()))
else:
print("Error! Word not found in your sentence.")

damn, u beat me to it Upside Down Stare
Last edited by Toxic ; 07-17-2016 at 07:18 AM.

The following user thanked Toxic for this useful post:

John

Copyright © 2024, NextGenUpdate.
All Rights Reserved.

Gray NextGenUpdate Logo