0

I'm trying to do a (a think) rather simple task with Python but cannot seem to do it or find a way to do it. I'm trying to have a for loop where my variables are set to another variable with a custom number. It's a bit hard to explain, but my code will explain it better :

aha_aha1 = "1"
aha_aha2 = "wouh"
aha_aha3 = "yes !"
test = "my man !"

for i in range (0, 4):
  if aha_aha{i} = "yes !":
     test[i] == aha_aha{i}.format(i)

I want test[i] to be aha_aha[i] only when certain conditions are met, but my code above obviously doesn't work. Any help ?

Vladyslav
  • 2,018
  • 4
  • 18
  • 44
CommYov
  • 29
  • 7

3 Answers3

1

If you really want those 'aha_x's:

d = {"aha_aha1" : "1", "aha_aha2" : "wouh", "aha_aha3" : "yes !"}
test = "my man !"

for i in range (0, 4):
  temp = "aha_aha"+str(i)
  if d.get(temp) == "yes !":
    #Do something
ksha
  • 2,007
  • 1
  • 19
  • 22
  • I'm getting my variables from an external source, and there are all like this something_1, something_2, etc... – CommYov Sep 21 '17 at 13:01
  • @CommYov if you know that the variables are going to arrive in some known fixed format, this should help – ksha Sep 22 '17 at 04:38
0

Actually, I don't understand what you are doing, but start from here at least:

aha_aha_list = ["1", "wouh", "yes !"]
test = "my man !"

for aha_aha in aha_aha_list:
    if aha_aha is "yes !":
        test = aha_aha

print(test)

Output:

yes !
Alperen
  • 3,772
  • 3
  • 27
  • 49
0

do you want that?in python str type is unable to change,so you should chage str to list,and then chat list to str.
d = {'aha_aha1': '1', 'aha_aha2': 'wouh', 'aha_aha3': 'yes !'}
test = "my man !"
for i in range(0, 4):
    if d.get("aha_aha{i}".format(i=i),None) == "yes !":
        test = list(test)
        test[i] = d.get("aha_aha{i}".format(i=i))
        test = ''.join(test)
        
        
   print(test) #my yes !an !
hys
  • 73
  • 5