Trying to swap the values of 2 variables that are chosen via input [python] | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

Trying to swap the values of 2 variables that are chosen via input [python]

So what I'm trying to do is to do is to create a function to exchange the values of 2 variables, the problem is that I want the variables to be chosen from a list by the input of the user. The first problem i found on my way is that if i to select a variable from a list is just going to choose the value of the variable. So value_var1=value_var2 doest mean var1=value_var2. Then I created a list with the variables names as strings and I tried to use eval() to make it into a function. The problem is that eval(var1)= value_var2 will give a syntax error. If anybody would be able to point me on the right direction it would be amazing. https://code.sololearn.com/cZ9Q3CEb6Isq/?ref=app

23rd Feb 2023, 1:47 PM
yara whatever
yara whatever - avatar
12 Answers
+ 4
Okay, so I suppose you would also access the stats by their name, like "strength", "awareness" or whatever, right? Variables are a good pick, when only your code itself needs to know the names. If there's some back and forth with the user involving the names, a *dictionary* might be a better pick than variables. stats = {'strength': 5, 'agility': 3} Now you can access the stats by using their name: print(stats['strength']) # output: 5 So if you ask the user for two inputs (which are already strings) - like: "which stats do you want to switch?", you can use these inputs directly and change it: stats[inp1], stats[inp2] = stats[inp2], stats[inp1]
23rd Feb 2023, 5:07 PM
HonFu
HonFu - avatar
+ 4
I'm not quite sure yet what you are trying there. It's about dice rolling in an RPG, I get that much... Can you tell something about the practical application? Why do you need to switch two values? (Knowing your purpose can make it easier finding the right solution.)
23rd Feb 2023, 4:08 PM
HonFu
HonFu - avatar
+ 3
Can you post your code please ?
23rd Feb 2023, 2:04 PM
Lamron
Lamron - avatar
+ 3
Good that you could make it work! As we are learning the parts of a language, we tend to make too many steps. Over time we then learn how to express the idea more concisely. It's generally a good idea to not create variables you don't need but create and use the values on the go. For example, instead of... result = randint(1,6) return result ... you can just write: return randint(1, 6) Or instead of... a, b, c, d= d6(),d6(),d6(),d6() y= [a,b,c,d] ... you could go with: y = [d(6), d(6), d(6), d(6)] Instead... sum=y[0]+y[1]+y[2] return sum ... use: return y[0]+y[1]+y[2] There would be ways to make it still shorter. I'm going from the tools you already use to illustrate that you don't create variables you won't use after that.
24th Feb 2023, 7:47 AM
HonFu
HonFu - avatar
+ 2
If you change part of your code to this; *** a = eval(a) + stat1 b = eval(b) + stat2 *** It will somehow work. Input has to be below 6 However. Your code is not optimised and can be simplified. Simply to swap values of 2 variables you can do something like: a = 4 b = 2 a, b = b, a
23rd Feb 2023, 2:46 PM
Lamron
Lamron - avatar
+ 2
yara whatever, feel free to follow up on it, if you have trouble figuring it out! You don't need to import anything, dict is a basic type, like lists, tuples, strings...
23rd Feb 2023, 5:45 PM
HonFu
HonFu - avatar
+ 1
So when you create a character in d&d, one of the ways to do it is to first roll 4d6 for each stat and add the 3 highest numbers. That would be the number on that stat. As when you do this its supposed to be done following the order the stats are, some games let you swap the result of 2 stats so you have some decision making into creating the character. Like if you want to be a barbarian and have 8 on strength and 15 on intelligence you could change it. You usually are allowed to do only one swap during character creation. I just wanted to see if I could emulate this as a way to practice and now I'm quite curious about how could this work.
23rd Feb 2023, 4:56 PM
yara whatever
yara whatever - avatar
+ 1
Thank you for your answer... I haven't reached yet the library part of the course. I will check the documentation to try to do it now and if it goes over my head I will try when I get there with sololearn. Really, thank you!
23rd Feb 2023, 5:44 PM
yara whatever
yara whatever - avatar
+ 1
Managed to make it work. At first I wanted to make something refering to the dictionary in this way: ******** stat1=stats[input()] stat2=stats[input()] stat1, stat2= stat2 stat1 ****** But then I understood that it would not change the values of key inside the library and would just exchange the values of stat1 and stat 2 (that are irrelevant outside of the function) so i went with this(that is the way you mentioned): ****** def stat_swap(): stat1=input() stat2=input() stats[stat1], stats[stat2]= stats[stat2], stats[stat1] ****** Also learning about dictionaries helped make the code much more simple. Thank you, again.
24th Feb 2023, 7:30 AM
yara whatever
yara whatever - avatar
0
Posted it, it's the second part of the code after the 3 lines of # that is giving me problems. Thanks for checking it
23rd Feb 2023, 2:23 PM
yara whatever
yara whatever - avatar
0
I tried to change the code but when I print the stats the second time the swap doesn't happen. It just keeps the same value. a, b = b, a seems great when you know which variables need to be exchange and you don't leave it to user choice. But when you want to leave it by choice inside a list seems to get crazy hard. About the optimization part , I'm still quite new (less than a week) in python so I'm still learning how to place code on the most simple way possible
23rd Feb 2023, 3:35 PM
yara whatever
yara whatever - avatar
0
Hii
24th Feb 2023, 2:53 PM
Altaf Pathan
Altaf Pathan - avatar