[英]How to repeat an input without while loop?
我正在弄清楚如何在我的列表中輸入 append ,問題是一些輸入是字符串,而另一些是浮點數,所以問題是是否可以將輸入迭代 2 次作為字符串和 3 次作為浮點數而無需花費一段時間/循環或 for 循環范圍,例如:
fighters = [[],[]]
for i in range(len(fighters)):
for j in range(5):
if j <= 1:
fighters[i].append(input())
else:
fighters[i].append(float(input()))
我不知道,做這樣的事情(這只是一個參考):
for i in range(len(fighters)):
fighters[i].append(input(), 2) # that 2 means 2 times for example
fighters[i].append(float(input(), 3) # and that 3 means 3 times
這是輸入的示例:
Gabriel # i want this 2 as strings
Wolf
7.5 # and this 3 as floats
5.1
5.1
Vlad # same here
Vampire
5.3
7.8
5.0
for i in range(len(fighters)):
for cls in (str,)*2 + (float,)*3:
fighters[i].append(cls(input()))
(str,)
是一個元素的元組,需要額外的逗號。 (str,) * 2
映射到(str, str)
所以(str,)*2 + (float,)*3
映射到(str, str, float, float, float)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.