简体   繁体   English

如何将用户输入保存到列表Python

[英]How to save user inputs into a list python

I am fairly new to python. 我是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列表中:

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. 并在循环结束时丢失其他append

This should work, but it's not very efficient for many replacements, as you're copying small variants of tempword many times. 这应该可以,但是对于许多替换来说效率不高,因为您多次复制了tempword较小变体。 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. 如果每个addpair1都是单个字符,则将tempword转换为可变序列(字符列表)会更快,使用dict进行替换。

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. 在多次替换的情况下,语义与原始代码并不相同,但是在这种情况下,它们可能会根据您所需的确切规范为您正确解决。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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