-1
count = 0

def checkletters(string):
    for letter in string:
        count +=1
input = input("What string do you want me to check for letter count: ")
checkletters(input)
print(f"There are {count} letters in that string")

I want the script to ask the user to input a string and it will send the amount of letters in the string

2 Answers2

1

Here's a better way:

def checkletters(string):
    count = 0
    for letter in string:
        count +=1
    return count
input = input("What string do you want me to check for letter count: ")
count = checkletters(input)
print(f"There are {count} letters in that string")
1

There are many ways to resolve this issue i have two for you

1. make you count variable global

global count

    count = 0
    def checkletters(string):
        for letter in string:
            global count
            count +=1
    input = input("What string do you want me to check for letter count: ")
    checkletters(input)

    print(f"There are {count} letters in that string")

2. By using count variable inside the function and returning value from that func..

def checkletters(string):
    count = 0
    for letter in string:
       count +=1
    return count 
input = input("What string do you want me to check for letter count: ")
checkletters(input)
print(f"There are {checkletters(input)} letters in that string")
loopassembly
  • 2,653
  • 1
  • 15
  • 22