简体   繁体   中英

How to save user inputs into a list python

I am fairly new to python. I am trying to make a decoding game where the player can guess the coded words. I have managed to ask the user which symbol they want to replace and what letter do they want to replace it with. However the next time that they replace a symbol, the previous substitution doesn't show. I know that i need to save my user inputs into a list, which i have created, but i am not really sure how. I was wondering if anyone could show me what i have to do. Here is my code for this bit:

subs2=[] # my list that i want to save the user inputs to 
while True:
    addpair1=input("Enter a symbol you would like to replace:") 
    addpair2=input("What letter would you like to replace it with:")

    for word in words_list:
        tempword = (word)
        tempword = tempword.replace('#','A')
        tempword = tempword.replace('*', 'M')
        tempword = tempword.replace('%', 'N')
        tempword=tempword.replace(addpair1,addpair2)
        print(tempword)
        subs2.append(tempword)

This is what happens when run this :

A+/084&" (the coded words)
A3MANA+
8N203:
Enter a symbol you would like to replace:+
What letter would you like to replace it with:k
Ak/084&"
A3MANAk   (the first substitution)
8N203:
Enter a symbol you would like to replace:J
What letter would you like to replace it with:/
A+/084&"
A3MANA+   ( the 2nd one)
8N203:
Enter a symbol you would like to replace:

As you can see the previous substitution doesnt save. I have looked online however, what i have tried doesnt work. I would be very grateful if anyone could help me

Right after your inputs, save in the subs2 list the pair just input:

subs2.append((addpair1, addpair2))

Then in your loop, where you now do just

tempword=tempword.replace(addpair1,addpair2)

do, instead, a secondary loop:

for ap1, ap2 in subs2:
    tempword=tempword.replace(ap1,ap2)

and lose the further append at the end of the loop.

This should work, but it's not very efficient for many replacements, as you're copying small variants of tempword many times. Turning tempword into a mutable sequence (a list of characters) will be faster, using a dict for the replacements, if each addpair1 is a single character as it appears.

So if that condition does hold I'd code, instead...:

subs2 = {'#':'A', '*':'M', '%':'N'}
while True:
    while True:
        addpair1=input("Enter a symbol you would like to replace:") 
        if len(addpair1) != 1:
            print('Just one character please, not {}'.format(len(addpair1)))
            continue
        if addpair1 in subs2:
            print('Symbol {} already being replaced, pick another'.format(addpair1))
            continue
        break
    addpair2=input("What letter would you like to replace it with:")
    subs2[addpair1] = addpair2

for word in words_list:
    tempword = list(word)
    for i, c in enumerate(tempword):
        tempword[i] = subs2.get(c, tempword[i])
    tempword = ''.join(tempword)
    print(tempword)

The semantics are not identical to your original code in the case of multiple substitutions, but they may work out correctly for you depending on your exact desired specs in such cases.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM