d2jsp
Log InRegister
d2jsp Forums > Off-Topic > Computers & IT > Programming & Development > Python Question > Halp
Add Reply New Topic New Poll
Member
Posts: 5,753
Joined: Aug 10 2015
Gold: 0.00
Nov 18 2019 11:37am
Hey guys,

I've got a python assignment that I'm trying to do but I have no idea how to start.

Write a function countLetters that expects 2 input arguments: a sentence and a letter. The function will check the number of times the letter occurs in the sentence. The function counterLetter should return a number (number of times the letter occurred in the sentence).


Write a function promptUser that prompts the user for a sentence and a letter. In promptUser, call countLetter with the sentence and letter you read in from the user. Save the value returned from the function and use in to append to a file count.txt.
Member
Posts: 5,753
Joined: Aug 10 2015
Gold: 0.00
Nov 18 2019 11:38am
Output should look like this


>>promptUser( )
Enter a sentence:Have a nice day!
Enter a letter:a


Open file count.txt and see:
3 a's found in Have a nice day!

------------------------------------------------
>>promptUser( )
Enter a sentence:CS118 is a great class!
Enter a letter:s

Open file count.txt and see:
3 a's found in Have a nice day!
3 s's found in CS118 is a great class!

--------------------------------------------------
>>promptUser( )
Enter a sentence:This is fun.
Enter a letter:u


Open file count.txt and see:
3 a's found in Have a nice day!
3 s's found in CS118 is a great class!
1 u's found in This is fun.

--------------------------------------------------
Enter a sentence:I can't wait for summer.
Enter a letter:z


Open file count.txt and see:
3 a's found in Have a nice day!
3 s's found in EGR115 is a great class!
1 u's found in This is fun.
0 z's found in I can't wait for summer.
Member
Posts: 17,591
Joined: Sep 27 2012
Gold: 3,260.00
Nov 18 2019 03:37pm

List = [letter for letter in sentence if (letter == chosenletter)]
Len(list)

This post was edited by ehmmkay on Nov 18 2019 03:37pm
Member
Posts: 17,311
Joined: Mar 21 2009
Gold: 3,604.22
Nov 18 2019 05:27pm
Idk what the python equivilant is, but it's just a Count function within a string.
Member
Posts: 772
Joined: Nov 11 2007
Gold: Locked
Nov 18 2019 06:36pm
Looks like you'll need understand how to
1. Get input from user
https://www.geeksforgeeks.org/taking-input-from-console-in-python/

2. Iterate characters in string with a for loop
https://www.geeksforgeeks.org/iterate-over-characters-of-a-string-in-python/

3. Write to a file
https://www.w3schools.com/python/python_file_write.asp

This post was edited by Tommy_The_Boy on Nov 18 2019 06:36pm
Member
Posts: 5,753
Joined: Aug 10 2015
Gold: 0.00
Nov 19 2019 08:45pm
Still have no idea
Member
Posts: 5,753
Joined: Aug 10 2015
Gold: 0.00
Nov 20 2019 09:12am
Figured it out. Thanks for the help guys
Go Back To Programming & Development Topic List
Add Reply New Topic New Poll