如何打印列表中每个项目的位置?
How do I print the position of each item in the list?
我希望将我选择的位置打印出来。我做了这个(在这里的帮助下)来帮助我。这段代码是为了让我轻松计算出等级奖励。人们告诉我他们处于什么级别,我会一个一个地输入这些级别,然后我会使用命令 (>add-money...) 将积分添加到他们的帐户中。在写出我给予的奖励时,我希望能够轻松地写出奖励来自哪个级别(即列表中的什么位置)
我怎样才能打印我使用的列表中的每个位置?
我的名单:
rewards = [0, 150, 225, 330, 500, 1000, 1500, 2250, 3400, 5000, 10000, 13000, 17000, 22000, 29000, 60000]
# 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
def rewardz():
# Running totals.
lists = []
total = 0
user=input('User -> ')
while True:
# Get reward level from the user. If not a valid reward level, stop.
level = input('-> ')
try:
level_num = int(level)
except ValueError:
break
if level_num not in range(len(rewards)):
break
# Add the reward to the lists and the total.
reward = rewards[level_num]
lists.append(reward)
total += reward
# Final output.
print(lists)
print(total, total*1000)
print()
print(" + ".join(str(i) for i in lists))
print('>add-money bank',user, total*1000)
print("\n-----########------\n\n")
rewardz()
rewardz()
我希望结果是什么(或类似于什么):
[2, 4, 7, 1, 4, etc]
由于您要求用户输入级别,因此您已经将级别保存在 level_num
中。如果你想 return 它你可以做 lists.append((reward, level_num))
或者为了一个可能更清洁的解决方案使用字典。
lists = {"rewards": [],
"level": []}
然后附加到它,你可以这样做:
lists["rewards"].append(reward)
lists["index"].append(level_num)
现在,在 lists["index"]
中,您拥有想要作为输出的列表,在 lists["rewards"]
中,您获得了您的值。
或者,您可以打开一个新列表并附加到该列表:
levels = []
levels.append(level_num) # In your while loop
我希望将我选择的位置打印出来。我做了这个(在这里的帮助下)来帮助我。这段代码是为了让我轻松计算出等级奖励。人们告诉我他们处于什么级别,我会一个一个地输入这些级别,然后我会使用命令 (>add-money...) 将积分添加到他们的帐户中。在写出我给予的奖励时,我希望能够轻松地写出奖励来自哪个级别(即列表中的什么位置)
我怎样才能打印我使用的列表中的每个位置?
我的名单:
rewards = [0, 150, 225, 330, 500, 1000, 1500, 2250, 3400, 5000, 10000, 13000, 17000, 22000, 29000, 60000]
# 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
def rewardz():
# Running totals.
lists = []
total = 0
user=input('User -> ')
while True:
# Get reward level from the user. If not a valid reward level, stop.
level = input('-> ')
try:
level_num = int(level)
except ValueError:
break
if level_num not in range(len(rewards)):
break
# Add the reward to the lists and the total.
reward = rewards[level_num]
lists.append(reward)
total += reward
# Final output.
print(lists)
print(total, total*1000)
print()
print(" + ".join(str(i) for i in lists))
print('>add-money bank',user, total*1000)
print("\n-----########------\n\n")
rewardz()
rewardz()
我希望结果是什么(或类似于什么):
[2, 4, 7, 1, 4, etc]
由于您要求用户输入级别,因此您已经将级别保存在 level_num
中。如果你想 return 它你可以做 lists.append((reward, level_num))
或者为了一个可能更清洁的解决方案使用字典。
lists = {"rewards": [],
"level": []}
然后附加到它,你可以这样做:
lists["rewards"].append(reward)
lists["index"].append(level_num)
现在,在 lists["index"]
中,您拥有想要作为输出的列表,在 lists["rewards"]
中,您获得了您的值。
或者,您可以打开一个新列表并附加到该列表:
levels = []
levels.append(level_num) # In your while loop