0

Here is the code:

name1 = input("Please enter a name: ")
name1age = int(input("Please enter ", name1, "'s age: "))

name2 = input("Please enter a name: ")
name2age = int(input("Please enter ", name2, "'s age: "))

name3 = input("Please enter a name: ")
name3age = int(input("Please enter ", name3, "'s age: "))

name4 = input("Please enter a name ")
name4age = int(input("Please enter ", name4, "'s age: "))

I want the value given for the name to be part of the new input.

Here is the error I'm recieving:

Traceback (most recent call last):#
File "\\albyn-sch\data\PupilHome\A.Shahrivar\My Documents\Computing 
Science\Python\Dance Group.py", line 2, in <module>
name1age = int(input("Please enter ", name1, "'s age: "))
TypeError: input expected at most 1 arguments, got 3

Any help? Thanks.

1
  • 1
    Pass arguments to the input as one line. Just replace , with +. Like this: input("Please enter " + name1 + "'s age: "). Commented Sep 4, 2018 at 9:42

2 Answers 2

3

You need to do string concatenation using +. See below:

name1 = input("Please enter a name: ")
name1age = int(input("Please enter " + name1 + "'s age: "))

This concatenates/joins the three string elements into one and passes it into the input function.

More information can be found here.

Sign up to request clarification or add additional context in comments.

1 Comment

Works fine for me, please make sure you have copied and pasted correctly.
0

Other alternatives:

You can use string format method:

name4 = input("Please enter a name ")
name4age = int(input("Please enter {}'s age: ".format(name4)))

Or f-strings you you are using python 3.6+:

name4 = input("Please enter a name ")
name4age = int(input(f"Please enter {name4}'s age: "))

You can also use the modulo operator (%) for string formatting but IMO format is better.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.