Question 1: I need to write a program that prompts a user for a sentence, and (u
ID: 3642464 • Letter: Q
Question
Question 1: I need to write a program that prompts a user for a sentence, and (using a for loop and .append method) prints a LIST of each unique character, with no repeats and no punctuation. My code so far is not doing the trick:mySentence = raw_input("Please input a sentence:")
myList = list(mySentence)
myList = sorted(myList)
print myList
alphStr = "abcdefghijklmnopqrstuvwxyz"
alphList = sorted(alphStr)
finalList = [ ]
for char in myList:
if char not in finalList:
finalList.append(myList)
print finalList
Question 2: How do I change this from a for loop to a while loop (it prints the number of a's in the string)
S = "I had a cat named amanda when I was little"
count = 0
for i in S:
if i == "a":
count += 1
print count
Thanks!
Explanation / Answer
you asked two questions.. rules only allow me answer one. here is the answer to the first
import string
s=raw_input("Enter a sentence ")
alplist=[]
for item in string.punctuation:
s=s.replace(item,"")
s=s.split()
for word in s:
for char in word:
if not (char in alplist):
alplist.append(char)
print sorted(alplist)
Related Questions
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.