#๐Ÿ”’ Functions

77 messages ยท Page 1 of 1 (latest)

warm raft
#

I'm learning how to do functions and I get an error for line 6.

def count_letter_e(word):
total_e = 0
for letter in word:
if letter == "e":
total_letter_e = total_letter_e + 1
return total_letter_e

user_name = input("Enter your name: ")
total_es_in_name = count_letter_e(user_name)
print("There are " + str(total_es_in_name) + "E's in your name")

charred otterBOT
#

@warm raft

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.

crimson light
#

the same with total_es_in_name

#

when you are doing for letter in word, you don't add the letterphrase onto the variable

pure portal
warm raft
#

what do i add?

warm raft
#

Traceback (most recent call last):
File "C:\Users\Pasha\AppData\Local\Programs\Python\Python312\test.py", line 9, in <module>
total_es_in_name = count_letter_e(user_name)
File "C:\Users\Pasha\AppData\Local\Programs\Python\Python312\test.py", line 6, in count_letter_e
return total_letter_e
UnboundLocalError: cannot access local variable 'total_letter_e' where it is not associated with a value

crimson light
#

yeah you never defined the variable

warm raft
#

oh ok

warm raft
#

what would i put instead of letter

crimson light
#

when you are doing for loops or anything, total_e remains the same, i believe you are trying to add onto it

warm raft
#

got it

crimson light
#

so rather than total_letter_e, and total_es_in_name, you should replace back total_e

warm raft
#

oh ok

crimson light
#

actually maybe not for total_es_in_name

warm raft
#

alright, ima try it real quick

crimson light
#

furtherly, there's another issue with your function

#

if letter == "e":

#

i don't think it worked, as i tested your code with the username "Apple", it didn't counted the E

warm raft
#

i tried it as well and it didn't work

crimson light
#

the issue is with this return total_e

#

when you executed the function, it iterates everything once

#

if letter == "e" checks for the first letter of the username, if it's not an E, it does not function

#

then it goes to the return statement

#

when you return something, it automatically exits the function

#

so the rest of the letters in the username were not reached

warm raft
#

How would I fix it so that it reads the whole username?

crimson light
#

also, the if statement only checks if it's a lowercase e or not, it does not include capital E

#

rather than putting the return statement inside of the for loop, as it will be executed every iteration

#

you can put it outside of the for loop, but still include it in the function

warm raft
#

I put it outside the loop but it still only did 0

crimson light
#

show me the code

warm raft
#
def count_letter_e(word):
    total_e = 0
    for letter in word:
        if total_e == "e":
            total_e = total_e + 1
    return total_e

user_name = input("Enter your name: ")
total_es_in_name = count_letter_e(user_name)
print("There are " + str(total_es_in_name) + " E's in your name")
#

something like this?

crimson light
#

the issue is here

#

if total_e =="e":

#

you're checking the letters, not the total_e

#

so it returns the original value of total_e = 0

warm raft
#

i think i forgot to replace the phrase letters aswell

crimson light
#

phrase letters?

warm raft
#

or word "letters"

crimson light
#

yeah that is what i meant

#

it should function properly when you replace it

#

the only issue is that it only detects for lowercase e, capital E's are not included in the detection

warm raft
#

I got an other error

#

Traceback (most recent call last):
File "C:\Users\Pasha\AppData\Local\Programs\Python\Python312\test.py", line 9, in <module>
total_es_in_name = count_letter_e(user_name)
File "C:\Users\Pasha\AppData\Local\Programs\Python\Python312\test.py", line 5, in count_letter_e
total_e = total_e + 1
TypeError: can only concatenate str (not "int") to str

crimson light
#

show me the full code again? i don't seem to find any issues with the code

#

you may have moved something

warm raft
#
def count_letter_e(word):
    total_e = 0
    for total_e in word:
        if total_e == "e":
            total_e = total_e + 1
    return total_e

user_name = input("Enter your name: ")
total_es_in_name = count_letter_e(user_name)
print("There are " + str(total_es_in_name) + " E's in your name")
crimson light
#

uh

#

you weren't supposed to change the letter into total_e

#
def count_letter_e(word):
    total_e = 0
    for letter in word:
        if letter == "e":
            total_e = total_e + 1
    return total_e

user_name = input("Enter your name: ")
total_es_in_name = count_letter_e(user_name)
print("There are " + str(total_es_in_name) + " E's in your name")
#

you're checking for the individual letters in the word, not checking fro total_e

crimson light
warm raft
#

but how do I make it so thst it checks for all the letter e

crimson light
#

if letter in "eE"

#

it checks if the letter is inside the string "eE"

#

if any of them exists, it executes the if statement

warm raft
#

still 0

crimson light
#

note : It's not if letter == "eE"

warm raft
#

oh

#

my fault

#

so i write in letter == "eE"

#

im just new at this

crimson light
#

if letter in "eE"

#

wrong order, wrong operation

warm raft
#

oh got it

#

It worked!

crimson light
#

congrats

#

!close to close

warm raft
#

thanks!

#

!close

charred otterBOT
#
Python help channel closed

This help channel has been closed and it's no longer possible to send messages here. If your question wasn't answered, 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.