Python 包含变量的输入函数提示

Python 包含变量的输入函数提示,python,variables,input,Python,Variables,Input,我有以下脚本: #! /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, 'y

我有以下脚本:

#! /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')
我想问的是年龄,包括姓名问题中的姓名,我的意思是,类似于:

age1 = int(input("Enter the age of", name1))
但这不起作用

如果你以第一人称John的身份回答,那么你应该得到:

进入约翰时代:

我该怎么做呢?

input()
将字符串作为其参数,因此只需使用变量创建一个字符串即可。例如,如果firstname=='John':

lastname = input('What is the last name of '+firstname+': ')
您需要连接字符串。你太接近了…

试试看

age1 = int(input("Enter the age of {}:".format(name1)))
或者,如果您更喜欢字符串插值:

age1 = int(input("Enter the age of %s:" % name1))

请让你的问题前后一致。。。您的变量是
name1
,但您随后谈到
nom1
。您是否尝试使用
+
而不是
?或者您已经演示过的
%
格式。脱离主题,但要交换变量,您可以使用
age1,age2=age2,age1
@TigerhawkT3,我的问题是针对输入函数的,您是对的,我已经知道“%formatting”,但我可能尝试了一些错误的方法。我认为这些答案对其他人很有用。但你可以随心所欲:)
age1 = int(input("Enter the age of %s:" % name1))