1

I assigned these variables:

x = "1"
name = str(input("Enter Name  :"))
gender = str(input("Enter Gender (M/F)  :")).lower
year = int(input("Enter Year of Birth  :"))   
postcode = str(input("Enter Postcode  :"))

Then I got I got a part of the string postcode.

partCode = postcode[0:3]

Then I converted the year in to a string.

birthYear = str(year)

After, I tried concatenating all the strings:

username = name + birthYear + gender + partCode + x

And I receive this error:

TypeError: can only concatenate str (not "builtin_function_or_method") to str

How can I solve it?

3
  • 1
    Why are you converting year to an int if you are just going to convert it back to a str in the first place? None of the calls to str are necessary, because input already returns a str. Commented Jan 14, 2020 at 20:59
  • 1
    As for your question, lower is a method, not a lowercase version of the original string. You need to call it: gender = input(...).lower(). Commented Jan 14, 2020 at 20:59
  • in addition to advise from @chepner, use string-formatting - e.g. f-strings or str.format() method, not concatenation. Commented Jan 14, 2020 at 21:01

3 Answers 3

3

enter image description here

The above error is because gender is not a string

replace

gender = str(input("Enter Gender (M/F)  :")).lower

with

gender = str(input("Enter Gender (M/F)  :")).lower()
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks! I realised what was wrong. You have been very helpful.
1

Try this :

x = "1"
name = str(input("Enter Name  :"))
gender = str(input("Enter Gender (M/F)  :")).lower()
year = int(input("Enter Year of Birth  :"))   
postcode = str(input("Enter Postcode  :"))

partCode = postcode[0:3]
birthYear = str(year)
username = name + birthYear + gender + partCode + x

1 Comment

Thanks! I realised the error and you have been really helpful!
0

You don't need to convert any of the input variables their all read in as str.

This will do

x = "1"
name = str(input("Enter Name  :"))
gender = str(input("Enter Gender (M/F)  :")).lower()
year = str(input("Enter Year of Birth  :"))   
postcode = str(input("Enter Postcode  :"))

The issue is you didn't have () for the lower function so your gender variable was <built-in method lower of str object at 0x7fb7152ffc38>. That should explain your error

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.