Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/google-apps-script/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python:类的代码有问题_Python - Fatal编程技术网

Python:类的代码有问题

Python:类的代码有问题,python,Python,提示: 许多用户创建的密码都很简单,很容易猜测。编写一个程序,使用一个简单的密码,通过使用下面的键替换字符,并在输入字符串的末尾添加“q*s”,使其更强大 i becomes ! a becomes @ m becomes M B becomes 8 o becomes . 例: 到目前为止我拥有的: #assign variables word = input() password = '' #checking for and modifying user's password if

提示: 许多用户创建的密码都很简单,很容易猜测。编写一个程序,使用一个简单的密码,通过使用下面的键替换字符,并在输入字符串的末尾添加“q*s”,使其更强大

i becomes !
a becomes @
m becomes M
B becomes 8
o becomes .
例:

到目前为止我拥有的:

#assign variables

word = input()

password = ''

#checking for and modifying user's password if containing the characters: i, a, m, B, or o
for character in word:

    if(character=='i'): #if user's password contains the letter i change to !
        word += '!'
    
    elif(character=='a'): #if user's password contains the letter a change to @
        word += '@'
        
    elif(character=='m'): #if the user's password contains the letter m change to M
        word += 'M'
        
    elif(character=='B'): #if the user's password contains the letter B change to 8
        word += '8'
        
    elif(character=='o'): #if the user's password contains the letter o change to .
        word += '.'
        
password = (word + 'qs')

#output final password

print(password)
我的输出搞砸了。我将密码输入为mypassword,输出为mypasswordM@.qs

有人能帮我告诉我哪里搞砸了吗?

你可以使用
.replace()

您还可以将所有
.replace()
放在一行

word = word.replace('i','!').replace('a','@').replace('m','M').replace('B','8').replace('o','.')

按照原始逻辑,您需要初始化一个单词以返回,否则所有字符都会附加到旧密码中

word = input()

password = ''

#checking for and modifying user's password if containing the characters: i, a, m, B, or o

for character in word: # update password rather than word

    if(character=='i'): #if user's password contains the letter i change to !
        password += '!'
    
    elif(character=='a'): #if user's password contains the letter a change to @
        password += '@'
        
    elif(character=='m'): #if the user's password contains the letter m change to M
        password += 'M'
        
    elif(character=='B'): #if the user's password contains the letter B change to 8
        password += '8'
        
    elif(character=='o'): #if the user's password contains the letter o change to .
        password += '.'
        
    else: # add else statement for when symbols do not get replaced 
        password += character

print(password)

您需要将字符附加到新变量,而不是正在处理的同一个变量。或者只使用
str.replace()
方法。您可以使用字典吗?您需要将结果分配回变量。谢谢!这非常有帮助。请不要只发布代码作为答案,还要解释代码的作用以及它如何解决问题。带有解释的答案通常更有帮助,质量更好,更容易吸引选票
word = word.replace('i','!').replace('a','@').replace('m','M').replace('B','8').replace('o','.')
word = input()

password = ''

#checking for and modifying user's password if containing the characters: i, a, m, B, or o

for character in word: # update password rather than word

    if(character=='i'): #if user's password contains the letter i change to !
        password += '!'
    
    elif(character=='a'): #if user's password contains the letter a change to @
        password += '@'
        
    elif(character=='m'): #if the user's password contains the letter m change to M
        password += 'M'
        
    elif(character=='B'): #if the user's password contains the letter B change to 8
        password += '8'
        
    elif(character=='o'): #if the user's password contains the letter o change to .
        password += '.'
        
    else: # add else statement for when symbols do not get replaced 
        password += character

print(password)
word = input()
password = ''

# Modify characters i, a, m, B, and o in password
for character in word:
    if character == 'i':     # IF character equals 'i' change to '!'
        password += '!'
    elif character == 'a':   # ELSE IF character equals 'a' change to '@'
        password += '@'
    elif character == 'm':   # ELSE IF character equals 'm' change to 'M'
        password += 'M'
    elif character == 'B':   # ELSE IF character equals 'B' change to '8'
        password += '8'
    elif character == 'o':   # ELSE IF character equals 'o' change to '.'
        password += '.'
    else:                    # When symbols do not get replaced 
        password += character

print(password + 'q*s')      # Make password stronger by adding "q*s" to the end of 
                             # the input string