在INPUT语句中包含一个变量

时间:2019-06-27 15:18:58

标签: python

我正在尝试使用一个内部变量创建输入语句。 这是一个从用户输入中获取多个名称的脚本

x = int(input("Numbers of names that'll be written: "))

for count in range(x):
    name = str(input("Insert the name number {}".format(count))) # <- This works but it's not what i want to achieve, since i don't know how to add a text after the .format
    # name = str(input("Insert the ",count,"st name: )) <- This doesn't work 

我希望输出:

Insert the 1st name; Insert the 2nd name; Insert the 3nd name;

2 个答案:

答案 0 :(得分:2)

  

我不知道如何在.format之后添加文本

就像您在{}前面有文字一样,您也可以在它后面有文字。

str(input("Insert the name number {}, whatever should come after 'cont'".format(cont)))

答案 1 :(得分:0)

我想您可能缺少序数后缀,可能有一种最简单(即更优化)的获取方法,但这只是一个开始:

def numberth(num: int):
    val = str(num)
    if val.endswith('1'):
        return val + 'st'
    if val.endswith('2'):
        return val + 'nd'
    if val.endswith('3'):
        return val + 'rd'
    return val + 'th'
相关问题