#π Issue
55 messages Β· Page 1 of 1 (latest)
@smoky night
Remember to:
- Ask your Python question, not if you can ask or if there's an expert who can help.
- Show a code sample as text (rather than a screenshot) and the error message, if you've got one.
- Explain what you expect to happen and what actually happens.
:warning: Do not pip install anything that isn't related to your question, especially if asked to over DMs.
d={}
word=input("Enter a word:")
for ch in word:
word=word.lower()
if ch.isalpha()== True and not ch in "":
d[word]=d.get(word,0)+1
word=input("Enter a word:")
if ch == "":
break
for w in d:
print(f'{w}:{d[w]}')
i dont know what the issue is
i want to get rid of the space
(the :3)
when there is nothing it s supposed to print how many times u wrote a certain word
to be clear, you want to
count how many times a certain word is repeated
it is not case-sensitive so the and The is same
and if input is empty, it should display the counts?
Exactly
can u show me ur latest code again? this output doesn't seem to be from the code u sent
d={}
word=input("Enter a word:")
for ch in word:
word=word.lower()
if ch.isalpha()== True and not ch in "":
d[word]=d.get(word,0)+1
word=input("Enter a word:")
if ch == "":
break
for w in d:
print(f'{w}:{d[w]}')
ok right now you are taking the first input outside the loop and the 2nd input inside the loop
it is better if you put the input in a separate loop so that it is executed from the same place every time
are you familiar with while loops?
yes
you can use a while loop to take the input an indefinite amount of times. later when you get an empty input, you can break out of it
so smth like this:
counter = {}
while True:
word = input('Enter a word: ')
now ch is undefined
we actually don't need ch
you are only trying to count the words not the charecters
so the whole loop over the word is useless
the for loop yes
what do i do with the if statement
lets first think about how we can stop if the input is empty cuz in that case we don't want to process it right?
we use break
so right after taking the word as input, we shouldcheck if its empty and break out of the loop if it is
if word =="":
yes
if word =="":
break
and i suppose you don't want to include whitespaces. either right? os if user entered " " you also want to stop?
Whats a white space?
just the charrecter that appears when u hit the space bar
" " that is called a whitespace
Yes
do u know about the strip() method?
yeah that will remove the whitespaces so " " becomes ""
you can use it while taking the input so whitespaces are removed at that time
counter = {}
while True:
word = input('Enter a word: ').strip()
I see
so now u can try putting in an empty string or a space and see if it stops or not
can i see your code?
do u understand how it works?
Yes
Itβs going to run the program until it hits false
Then when the word is nothing
We break out of the while loop
Right?
This help channel has been closed. Feel free to create a new post in #1035199133436354600. To maximize your chances of getting a response, check out this guide on asking good questions.