Python3.x-如何获取整数变量并将其转换为大量输入?
假设您有这样一个代码:Python3.x-如何获取整数变量并将其转换为大量输入?,python,python-3.x,variables,input,integer,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 # '
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= 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.
此外,如果改变输入数量的“形状”变量也改变了。因此:
>>>
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.
如果序号对输出很重要(“第一个字母”、“第二个字母”、“第三个字母”等),那么我会:
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")
使用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 + ".")
因此,我最终使用@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 + ".")
num=0
活套=形状+1
name=“”
对于范围内的n(活套):
num+=1
定义到顺序(n:int)->str:
结尾={1:“st”,2:“nd”,3:“rd”}
如果11 1:
name=name+input(f“第一个形状名称中的{to_序数(n)}字母是什么?:”)
#f“{some_var}”语法仅在py3.6中有效+
#否则使用`inputs.append(“命名{}端:”.format(to_序数(n))`
打印(“\n您的第一个”,shape+”的名称为“+name+”)
请检查@cameronroytaylor和@AdamSmith的回答,因为他们帮助得出了这些答案。他们值得表扬。范围(X)内的构造
,
将在封闭的块X
上循环。希望这会有帮助,因为我不理解你的例子(你的第二个代码块指出应该有5个提示,但只提示了4次…)出于好奇——为什么要使用字符串呢?@AdamSmith我将这个问题解释为#ObGamErOGE关心将用户输入附加到一个名称中。你的代码非常适合生成输入函数的输入(提示)。使用这两个代码片段#ObGamErOGE应该能够实现他们的目标。这很公平!明智地说:字符串连接效率非常低。如果形状
变得非常大,这可能会导致问题。如果你积累到一个列表中,那么使用'.join(该列表)
您以更便宜的价格获得了相同的输出。您是如何做到的。我的意思是它工作得很好,但我不明白为什么。请任何人向我解释它是如何工作的。我编辑了代码,以便有注释解释它是如何工作的。如果有任何不合理的地方,请告诉我,我将为您解释。此外,您可能希望更改将input()
参数中的i
转换为(i+1)
,如果您希望它从1
开始,而不是从0
@fuzzything44开始,感谢您的编辑。看起来我们都错过了一些边缘案例(您的代码捕捉到了“22”,但不正确地给出了“12”)我认为我当前的编辑是正确的。我得到这个错误的原因是:回溯(最近一次调用last):文件“-------------------------------”,第19行,在范围(1,num_inputs+1)内的n中:TypeError:必须是str,而不是strint@ObGamErOGE哦,因为我很笨,没有将用户输入转换为int:)