Python 3.x - 如何获取整数变量并将其转换为多个输入?

时间:2017-05-12 19:38:57

标签: python python-3.x variables input integer

所以让我们假设您有这样的代码:

shape= int(input("How many sides are in your shape?: "))
print("So you have", shape,"number of sides.")

现在我想做的是取用户输入的内容(当然是整数)并根据用户输入的内容创建一些输入。像这样:

>>>
How many sides are in your shape?: 3
So you have 3 number of sides.
What is the 1st letter in your shape's name?: A 
# 'That is an input that I cannot figure out'
What is the 2nd letter in your shape's name?: B 
# 'That is an input that I cannot figure out'
What is the 3rd letter in your shape's name?: C
# 'That is an input that I cannot figure out'
So your shape's name is ABC. 

此外,如果改变输入数量的'shape'变量也会发生变化。所以:

>>>
How many sides are in your shape?: 5
So you have 3 number of sides.
What is the 1st letter in your shape's name?: E
# 'That is an input that I cannot figure out'
What is the 2nd letter in your shape's name?: F
# 'That is an input that I cannot figure out'
What is the 3rd letter in your shape's name?: H
# 'That is an input that I cannot figure out'
What is the 4th letter in your shape's name?: G
# 'That is an input that I cannot figure out'
So your shape's name is EFHG. 

3 个答案:

答案 0 :(得分:2)

如果序数对输出很重要(“第一个字母”,“第二个字母”,“第三个字母”等),那么我会这样做:

def to_ordinal(n:int) -> str:
    """takes a number and returns its ordinal

    >>> to_ordinal(1)
    1st
    >>> to_ordinal(155)
    155th
    """

    endings = {1: "st", 2: "nd", 3: "rd"}

    if 11 <= n < 20:
        return str(n) + "th"
    else:
        return str(n) + endings.get(n % 10, "th")

然后将输入包装在带有累加器列表的for循环中。

inputs = []
num_inputs = int(input("How many sides? "))
for n in range(1, num_inputs+1):
    inputs.append(f"Name your {to_ordinal(n)} side: ")
    # the f"{some_var}" syntax only works in py3.6+
    # otherwise use `inputs.append("Name your {} side: ".format(to_ordinal(n))`

答案 1 :(得分:0)

使用shape迭代并附加到字符串。

# Create an empty string.
name = ""
# For indices 0 through the length of shape minus 1,
for i in range(shape):
    # request user input for that index and add the letter inputted to the end of the string.
    name = name + input("Input letter %d: " % i)
# Then print the string.
print("\nYour shape's name is " + name + ".")

答案 2 :(得分:0)

所以我最终做的是使用@cameronroytaylor和@AdamSmith的回复来创建这段代码。

shape= int(input("How many sides are in your shape?: "))
print("So you have", shape,"number of sides.")

然后添加了这个:

num = 0

looper = shape+1

name = ""

for n in range(looper):
    num += 1
    def to_ordinal(n:int) -> str:

        endings = {1: "st", 2: "nd", 3: "rd"}

        if 11 <= n < 20:
            return str(n) + "th"
        else:
            return str(n) + endings.get(n % 10, "th")

    if num > 1:
        name = name + input(f"What is the {to_ordinal(n)} letter in your first shape's name?: ")
    # the f"{some_var}" syntax only works in py3.6+
    # otherwise use `inputs.append("Name your {} side: ".format(to_ordinal(n))`

print("\nYour first", shape+"'s name is " + name + ".")

请检查@cameronroytaylor和@ AdamSmith的回复,因为他们帮助我们提出了这些答案。他们应该得到信任。