Alana Gooz
Alana Gooz

Reputation: 23

'method' object is not iterable

I am writing my code, and trying to reassign the input variable (YourName) to only include the letters, and to disregard any numbers (if that makes sense)

while YourName == None:
    YourName = (input("But before we begin, what is your name?:")) #The name can be anything.
    YourName = YourName.capitalize()
    YourName = ''.join(filter(str.isalpha(YourName), input))

But when I run it comes up with 'method' not iterable.

How do I fix this

Upvotes: 2

Views: 3040

Answers (2)

Krishna
Krishna

Reputation: 1362

If you want to use filter, the line should be as follows

YourName = ''.join(filter(str.isalpha, YourName))

Full program

YourName = None

while YourName == None:
    YourName = (input("But before we begin, what is your name?:")) #The name  can be anything.
    YourName = YourName.capitalize()
    YourName = ''.join(filter(str.isalpha, YourName))
    print(YourName)

Sample output

But before we begin, what is your name?:1231af 45 sdfsd
afsdfsd

NOTE: You are missing out the spaces

Upvotes: 1

Krishna
Krishna

Reputation: 1362

You can use re

import re

YourName = None

while YourName == None:
    YourName = (input("But before we begin, what is your name?:")) #The name  can be anything.
    YourName = YourName.capitalize()
    YourName = re.sub(r'\d+', '', YourName)
    print(YourName)

Sample output

But before we begin, what is your name?:ada64782jhadas
Adajhadas

Upvotes: 0

Related Questions