使用变量[closed]替换功能

问题描述 投票:-2回答:2

我需要从用户处输入字符串,然后询问用户他们希望看到的哪个字符消失...然后在缺少所选字符的情况下打印其字符串。

这是我到目前为止的代码。奇怪的是程序启动并运行,但随后返回语法错误:

user_input = ("Please enter a string")
disappear = ("now please enter the chracter you would like to disappear.") 
user_input = user_input.replace(str(disappear), (''))
print (user_input)

请原谅我的缩进。这是在我的手机上完成的。

python replace str-replace
2个回答
0
投票

根据以上代码,您不会在任何地方进行输入。使用input()功能进行输入。就您的代码而言,语法为:

user_input = input("Please enter a string") 
disappear = input("now please enter the chracter you would like to disappear.") 
user_input = user_input.replace(str(disappear), '') 
print(user_input)

0
投票
user_input = input("Please enter a string")
disappear = input("now please enter the chracter you would like to disappear.") 
user_input = user_input.replace(str(disappear), (''))
print (user_input)
© www.soinside.com 2019 - 2024. All rights reserved.