#πŸ”’ Issue

55 messages Β· Page 1 of 1 (latest)

spice iceBOT
#

@smoky night

Python help channel opened

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.

smoky night
#
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

latent sphinx
latent sphinx
# smoky night

can u show me ur latest code again? this output doesn't seem to be from the code u sent

smoky night
#
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]}')
latent sphinx
#

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?

smoky night
#

yes

latent sphinx
# smoky night 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: ')
smoky night
#

now ch is undefined

latent sphinx
#

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

smoky night
#

this part?

latent sphinx
#

the for loop yes

smoky night
#

what do i do with the if statement

latent sphinx
#

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?

smoky night
#

we use break

latent sphinx
#

so right after taking the word as input, we shouldcheck if its empty and break out of the loop if it is

smoky night
#
if word =="":
latent sphinx
#

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?

smoky night
#

Whats a white space?

latent sphinx
#

just the charrecter that appears when u hit the space bar
" " that is called a whitespace

smoky night
#

Yes

latent sphinx
#

do u know about the strip() method?

smoky night
#

Yes

#

Word.strip()?

latent sphinx
#

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()
smoky night
#

I see

latent sphinx
#

so now u can try putting in an empty string or a space and see if it stops or not

smoky night
#

Ok

#

thank you

#

it works

latent sphinx
#

can i see your code?

latent sphinx
smoky night
#

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?

spice iceBOT
#
Python help channel closed for inactivity

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.