简体   繁体   English

如何将变量打印为字符串形式的输入长度?

[英]How do you print a variable as a string for the length of an input?

my task is when I type 'z' i get the outcome of 'w'. 我的任务是当我输入“ z”时得到“ w”的结果。

This works. 这可行。

However, if I input 'zzzz' only a single 'w' is outputted. 但是,如果我输入“ zzzz”,则仅输出一个“ w”。

My question is how can I print 'w' for the number of times I enter z. 我的问题是如何输入“ z”的次数打印“ w”。

I am relatively new to StackOverflow. 我对StackOverflow比较陌生。 I'm sorry if I have broken any rules or my question is not correctly phrased. 很抱歉,如果我违反了任何规则或我的问题措辞不正确。

z='w'
while True:
    plaintext=input('enter a word to get its ciphertext')            
    i=list(plaintext)
    print (i)
    if 'z' in plaintext
        print("w")

You are overcomplicating it. 您太复杂了。 The variable you get from input() is a string. 您从input()获得的变量是一个字符串。 Simply use str.replace() . 只需使用str.replace()即可

while True:
    plaintext = input('enter a word to get its ciphertext')
    plaintext_rep = plaintext.replace('z','w')
    print(plaintext_rep)

Try looping through the text to get all of the letters 尝试遍历文本以获取所有字母

z='w'
while True:
    plaintext=input('enter a word to get its ciphertext')
    i=plaintext

    for letter in i :
        if(letter == 'z'):
            print ('w')

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

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