Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Splitting a list help
#1
I have been coding python 3 using repl.it, and I have this code. To make everything work I had it print to make sure it worked. For some reason, Stats wont split as part of health.

Stats = [5, 5, 5, 5, 5],
Head = 'Leather Cap',
Torso = 'Leather Vambrace',
Arms = 'Leather Shoulder Pads',
LeftHand = 'Iron Longsword',
RightHand = 'Wooden Buckler',
Legs = 'Leather Pants',
Feet = 'Leather Boots',
PlayerOneStats = {}
PlayerOneHealthCap = Stats[0]
print(PlayerOneHealthCap)
Help please!

Moderator: Added code tags for you this time. Please use in future.
Reply
#2
Quote:Stats wont split as part of health
stats is a list. It is already split. More info..
Recommended Tutorials:
Reply
#3
What I mean is that when it prints the health part it still print the entire list of 5s without taking just the first one or multiplying it. How can I remedy this?
Reply
#4
list[0] will return the first element of the list
>>> stats = [1,2,3,4,5,6,7,8]
>>> stats[0]
1
Quote:Stats = [5, 5, 5, 5, 5],
Head = 'Leather Cap',
Torso = 'Leather Vambrace',
Arms = 'Leather Shoulder Pads',
LeftHand = 'Iron Longsword',
RightHand = 'Wooden Buckler',
Legs = 'Leather Pants',
Feet = 'Leather Boots',
What is up with the commas after each line?
Recommended Tutorials:
Reply
#5
You have an extra comma at the end of  Stats = [5, 5, 5, 5, 5], so Python thinks Stats is a one-element tuple, whose first element is [5, 5, 5, 5, 5]. So Stats[0] is [5, 5, 5, 5, 5].... Remove the comma and it should work as expected.
Unless noted otherwise, code in my posts should be understood as "coding suggestions", and its use may require more neurones than the two necessary for Ctrl-C/Ctrl-V.
Your one-stop place for all your GIMP needs: gimp-forum.net
Reply
#6
Doh
Recommended Tutorials:
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Splitting strings in list of strings jesse68 3 1,704 Mar-02-2022, 05:15 PM
Last Post: DeaD_EyE
  Splitting String into 2d list cclark135 2 2,757 Aug-26-2019, 01:46 PM
Last Post: ThomasL
  splitting numeric list based on condition python_newbie09 7 9,281 May-27-2019, 03:58 PM
Last Post: python_newbie09
  Need help | splitting list into list Vinci141 3 2,551 Mar-13-2019, 09:09 PM
Last Post: Vinci141
  some ideas for intelligent list splitting? wardancer84 4 3,180 Nov-20-2018, 02:47 PM
Last Post: DeaD_EyE
Question Code golfing: splitting a list Ofnuts 7 6,045 Jan-12-2017, 11:01 PM
Last Post: wavic

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020