0

String inputs- list- dict? or classes? Multiply attributes (int) by amount of each type - print totals of attributes

I tried a list of dictionaries and .split() my user input into a list but I can't use the list items to work with my dictionaries. I originally started by making classes but was drawn to dictionaries because they seemed to be be better? I don't even know. A typical input for my dict code would be "guard1 guard2" https://code.sololearn.com/cb6OLdBB0QA7/?ref=app https://code.sololearn.com/chKNhh1tTB03/?ref=app

10th Mar 2023, 12:34 PM
Travis Beard
Travis Beard - avatar
16 ответов
10th Mar 2023, 2:03 PM
Ausgrindtube
Ausgrindtube - avatar
+ 3
Ah ok! This might be better used for that purpose. https://docs.python.org/3/howto/enum.html
10th Mar 2023, 2:43 PM
Ausgrindtube
Ausgrindtube - avatar
10th Mar 2023, 3:56 PM
Bob_Li
Bob_Li - avatar
+ 2
Bob_Li wow that's completely different, I'm gonna reverse engineer that for a while. Thanks
10th Mar 2023, 4:00 PM
Travis Beard
Travis Beard - avatar
+ 1
I'm gonna read up on it, thanks.
10th Mar 2023, 3:19 PM
Travis Beard
Travis Beard - avatar
+ 1
I got it to accept user input and I'm trying to fix the totals I made and take multiple inputs. My brain is toast for the day though. https://code.sololearn.com/chhb51KNkCL9/?ref=app
10th Mar 2023, 8:02 PM
Travis Beard
Travis Beard - avatar
+ 1
Travis Beard you can't define the function inside a while loop. Also use if instead of while. maybe you should also define a setter function. The one flaw in my code below is that modifying the globals() is not good practice. While it is possible to change the "name" inside the dict, the variable name you assigned the dict to is not simultaneously changed. So now, the two don't match.😅 Which might be problematic because you're searching via variable name stored in the globals(). There is a way to synchronize the variable name and dictionary "name" key, but that would involve more hacks into globals(). Which is basically what you would be doing when creating classes in OOP. So it's back to square one...🙃 Try this https://code.sololearn.com/cOiHMQhlleyG/?ref=app
11th Mar 2023, 1:39 AM
Bob_Li
Bob_Li - avatar
+ 1
Travis Beard hacking the globals. At this point, the code is getying so convoluted, I might as well go back to OOP😅 https://code.sololearn.com/cIn3R63FN7Ln/?ref=app
11th Mar 2023, 5:06 AM
Bob_Li
Bob_Li - avatar
+ 1
I quit the OOP right at a setter function haha thought it might have been over complicated. I'm glad to have you on board because I only understand about a quarter of what you said last. Gotta digest again.
11th Mar 2023, 6:37 AM
Travis Beard
Travis Beard - avatar
+ 1
I think I mentioned somewhere in here thinking of using a setter like they do in the password lesson to verify the input and then use something that's disconnected from the input to access the class data. I don't know for sure but I would think that would change input from the captain of the ship to just the door man
11th Mar 2023, 6:42 AM
Travis Beard
Travis Beard - avatar
+ 1
Would a lambda do anything? 🤔 we can't define a function inside the loop, but this says a lambda can do it "on the fly"
11th Mar 2023, 7:34 AM
Travis Beard
Travis Beard - avatar
0
Yes I just can't figure out how to use user input to select which items are added and multiplied
10th Mar 2023, 2:08 PM
Travis Beard
Travis Beard - avatar
0
Like if I say x = input() Input gaurd1 Return or print guard1 I don't want the string "guard1" I want to work with the output you produced so that I can multiply by how many of each and then add them all together so I know all the stats of my stack of soldiers combined
10th Mar 2023, 2:11 PM
Travis Beard
Travis Beard - avatar
0
Eventually I want to be able to enter guard1 then it ask how many then prompt to add additional units and how many till I'm done but I'm stuck on fundementals (guard1 would mean one level one guard, eventually I wanna be able to add the stats categories of say 3 level 4 guard and 2 level 3 tank destroyer and see the cumulative HP, atk etc)
10th Mar 2023, 2:14 PM
Travis Beard
Travis Beard - avatar
10th Mar 2023, 8:12 PM
Travis Beard
Travis Beard - avatar
11th Mar 2023, 9:25 AM
Travis Beard
Travis Beard - avatar