2015-10-20 73 views
-1

我有以下脚本:提示输入功能,包括变量

#! /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)) 

但是,这并不工作...

所以,如果你回答,因为第一personame约翰,所以你应该得到:

输入John的年龄:

我怎样才能做到这一点?

+0

请你的问题是一致......你的变量是'name1',但你再谈论'nom1'。 – donkopotamus

+0

您是否尝试使用'+'代替'','? – TigerhawkT3

+0

或者你已经知道的'%'格式。 – TigerhawkT3

回答

1

尝试

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

,或者如果你喜欢字符串插值:

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

input()需要一个字符串作为它的参数,所以只需创建一个字符串与您的变量。即,如果firstname == 'John'

lastname = input('What is the last name of '+firstname+': ') 
0
age1 = int(input("Enter the first age " + name1)) 

您需要在连接字符串。你是如此接近......