Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in Data Science by (18.4k points)

This is my code:

import random

stats=[]

attributes=5

print("Stats:", end=" ")

for i in range(attributes):

  r=random.randint(40,70)

  stats.append(r)

  print(stats[i], end=" ")

print('''

\t[1]- Strength

\t[2]- Dexterity

\t[3]- Intelligence

\t[4]- Wisdom

\t[5]- Charisma

''')

select = int(input("Select: "))

select -= 1

stats[select] += random.randint(10,15)

for i in range(len(stats)):

  if i==select:

    continue

  stats[i] -= random.randint(10,15)

print(f'''New Stats is: {stats}''', end= " ")

#Fireball

fireball=[12, 15, 28, 10, 5]

skill=input("\nChoose One Of Four Skills: ")

if skill=="F" or "f":

  stats[i] -= fireball[i]

  if stats[i]>0:

    print(stats)

#After this there should be 3 more similar ones (Like same but for lightning).

#Lightning

#Silence

#Fire Ward

With this code I get for example this output:

Stats: 65 48 48 42 41 

    [1]- Strength

    [2]- Dexterity

    [3]- Intelligence

    [4]- Wisdom

    [5]- Charisma

Select: 3

New Stats is: [54, 36, 59, 29, 26] 

Choose One Of Four Skills: f

[54, 36, 59, 29, 21]

However, what I expect to see in the last lines are:

Choose One Of Four Skills: f

[42, 21, 28, 19, 16]

As per my knowledge, a list should be decreased by a value in my list fireball in these lines:

if skill=="F" or "f":

      stats[i] -= fireball[i]

Can anyone please explain to me why I am getting the same list? I tried to add [i] in the last line (which is: print(stats)), but in that case, it shows only one value.

1 Answer

0 votes
by (36.8k points)

You're using the i from a previous loop. Create the new loop to adjust the stats.

#Fireball

fireball=[12, 15, 28, 10, 5]

skill=input("\nChoose One Of Four Skills: ")

if skill=="F" or "f":

  for i in range(len(stats)):

    stats[i] -= fireball[i]

  print(stats)

Output

Stats: 51 57 54 61 65

        [1]- Strength

        [2]- Dexterity

        [3]- Intelligence

        [4]- Wisdom

        [5]- Charisma

Select: 3

New Stats is: [38, 44, 68, 46, 50]

Choose One Of Four Skills: f

[26, 29, 40, 36, 45]

Want to be a master in Data Science? Enroll in this Data Science Courses

Browse Categories

...