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
16 Antworten
+ 6
Do you mean like this?
https://code.sololearn.com/cS3J8um677qc/?ref=app
+ 3
Ah ok!
This might be better used for that purpose.
https://docs.python.org/3/howto/enum.html
+ 3
Travis Beard
how about something like this?
https://code.sololearn.com/cfY4p492u6Q5/?ref=app
+ 2
Bob_Li wow that's completely different, I'm gonna reverse engineer that for a while. Thanks
+ 1
I'm gonna read up on it, thanks.
+ 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
+ 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
+ 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
+ 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.
+ 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
+ 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"
0
Yes I just can't figure out how to use user input to select which items are added and multiplied
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
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)
0
from when It worked last..
https://code.sololearn.com/cll5c91FzMdx/?ref=app
0
back to the loop part
https://code.sololearn.com/cifKb1WnwOks/?ref=app