多行输入在python中不同的输出

问题描述 投票:0回答:1

我是一名新的自定进度的学生,尝试学习python(3.x.x)。我正在尝试使用一个变量,用于接受不同的输入(名称)并将其输出到打印语句中。

reference = input('Who is your first reference: ') 
reference = input('Who is your second reference: ')

print ('Your first reference is: ' + reference)
print ('Your second reference is: ' + reference)

所需的输出

Your first reference is: Thabo Mbeki
Your second reference is: Allan Turig
python input printing output
1个回答
1
投票

要在同一个变量中存储两个元素,您将使用一个列表。

例如:

reference = []

reference.append(input('Who is your first reference: ')) 
reference.append(input('Who is your second reference: '))

print ('Your first reference is: ' + reference[0])# List start at 0. the 1st element
print ('Your second reference is: ' + reference[1])# 2nd element

0
投票

最简单的方法是将不同的输入分配给不同的变量:

reference_1 = input('Who is your first reference: ') 
reference_2 = input('Who is your second reference: ')

print ('Your first reference is: {}'.format(reference_1))
print ('Your second reference is: {}'.format(reference_2)

输出:

Your first reference is: Thabo Mbeki
Your second reference is: Allan Turig
© www.soinside.com 2019 - 2024. All rights reserved.