-1

I have the following script:

#! /usr/bin/python3
name1 = input('Enter the name of first person ')
name2 = input('Enter the name of second person ')
age1 = int(input("Enter the first age "))
age2 = int(input('Enter the second age '))

print('%s' %name1,  'is %d' %age1, 'years and %s' %name2,  'is %d' %age2, 'years')

agex = age1

age1 = age2
age2 = agex


print('Now we swap ages: %s' %name1,  'is %d' %age1, 'years and %s' %name2,  'is %d' %age2, 'years')

What I'd want is to ask for ages including the name entering in the name questions, I mean, something like:

age1 = int(input("Enter the age of", name1))

But that does not work...

So if you answer as first personame John, so you should get:

Enter the age of John:

How can I do that?

donkopotamus
  • 22,114
  • 2
  • 48
  • 60
sebelk
  • 565
  • 1
  • 6
  • 16

3 Answers3

1

Try

age1 = int(input("Enter the age of {}:".format(name1)))

or if you prefer string interpolation:

age1 = int(input("Enter the age of %s:" % name1))
donkopotamus
  • 22,114
  • 2
  • 48
  • 60
0

input() takes a string as its parameter so just create a string with your variable. ie, if firstname == 'John':

lastname = input('What is the last name of '+firstname+': ')
R Nar
  • 5,465
  • 1
  • 16
  • 32
0
age1 = int(input("Enter the first age " + name1))

You need to concatenate the strings. You were so close ...

donkopotamus
  • 22,114
  • 2
  • 48
  • 60
Prune
  • 76,765
  • 14
  • 60
  • 81