Python:如何在打印前累积

Python:如何在打印前累积,python,python-2.7,Python,Python 2.7,我试图编写一个程序(没有函数),当我键入hello时,它会打印h0ll0,但我得到的是h0llo,后跟hell0。我们还没有找到函数,所以我尝试在不使用它们的情况下编写此代码,但在打印h0ll0之前,我不知道如何进行累积。有人能解释一下我做错了什么吗?你之所以会得到h0llo和hell0,是因为你在检查vows中的每个元音,并在每次替换时打印出字符串 这就是正在发生的事情: vows = "aeiouy" myInput = raw_input("Insert a phrase containi

我试图编写一个程序(没有函数),当我键入
hello
时,它会打印
h0ll0
,但我得到的是
h0llo
,后跟
hell0
。我们还没有找到函数,所以我尝试在不使用它们的情况下编写此代码,但在打印
h0ll0
之前,我不知道如何进行累积。有人能解释一下我做错了什么吗?

你之所以会得到
h0llo
hell0
,是因为你在检查
vows
中的每个元音,并在每次替换时打印出字符串

这就是正在发生的事情:

vows = "aeiouy"
myInput = raw_input("Insert a phrase containing vowels that you want to nullify: ")
lowerCased = myInput.lower()
for vow in vows:
    if vow in lowerCased:
            print lowerCased.replace(vow, "0")
循环启动

“你好”中的a?
False

e在“hello”中?
True
替换hello中的e;变成h0llo

i在“hello”中?
False

o在“hello”中?
True
替换hello中的o;变成hell0

u在“hello”中?
False

循环结束。

此外,不需要使用小写的
if-vow。您可以通过以下方式重写代码:

for vow in vows: # checks each vowel
    if vow in lowerCased:
            print lowerCased.replace(vow, "0")

您之所以得到
h0llo
hell0
是因为您正在检查
vows
中的每个元音,并在每次替换时打印字符串

这就是正在发生的事情:

vows = "aeiouy"
myInput = raw_input("Insert a phrase containing vowels that you want to nullify: ")
lowerCased = myInput.lower()
for vow in vows:
    if vow in lowerCased:
            print lowerCased.replace(vow, "0")
循环启动

“你好”中的a?
False

e在“hello”中?
True
替换hello中的e;变成h0llo

i在“hello”中?
False

o在“hello”中?
True
替换hello中的o;变成hell0

u在“hello”中?
False

循环结束。

此外,不需要使用小写的
if-vow。您可以通过以下方式重写代码:

for vow in vows: # checks each vowel
    if vow in lowerCased:
            print lowerCased.replace(vow, "0")

您之所以得到
h0llo
hell0
是因为您正在检查
vows
中的每个元音,并在每次替换时打印字符串

这就是正在发生的事情:

vows = "aeiouy"
myInput = raw_input("Insert a phrase containing vowels that you want to nullify: ")
lowerCased = myInput.lower()
for vow in vows:
    if vow in lowerCased:
            print lowerCased.replace(vow, "0")
循环启动

“你好”中的a?
False

e在“hello”中?
True
替换hello中的e;变成h0llo

i在“hello”中?
False

o在“hello”中?
True
替换hello中的o;变成hell0

u在“hello”中?
False

循环结束。

此外,不需要使用小写的
if-vow。您可以通过以下方式重写代码:

for vow in vows: # checks each vowel
    if vow in lowerCased:
            print lowerCased.replace(vow, "0")

您之所以得到
h0llo
hell0
是因为您正在检查
vows
中的每个元音,并在每次替换时打印字符串

这就是正在发生的事情:

vows = "aeiouy"
myInput = raw_input("Insert a phrase containing vowels that you want to nullify: ")
lowerCased = myInput.lower()
for vow in vows:
    if vow in lowerCased:
            print lowerCased.replace(vow, "0")
循环启动

“你好”中的a?
False

e在“hello”中?
True
替换hello中的e;变成h0llo

i在“hello”中?
False

o在“hello”中?
True
替换hello中的o;变成hell0

u在“hello”中?
False

循环结束。

此外,不需要使用小写的
if-vow。您可以通过以下方式重写代码:

for vow in vows: # checks each vowel
    if vow in lowerCased:
            print lowerCased.replace(vow, "0")
my_string.replace(“a”、“b”)
返回一个字符串,其中
a
b
替换。因此,您可以存储该值并每次替换它,然后在所有替换之后打印它

vows = "aeiouy"
myInput = raw_input("Insert a phrase containing vowels that you want to nullify: ")
lowerCased = myInput.lower()
for vow in vows:
  lowerCased = lowerCased.replace(vow, '0') # overwrites lowerCased with the updated replacement
print(lowerCased)
my_string.replace(“a”、“b”)
返回一个字符串,其中
a
b
替换。因此,您可以存储该值并每次替换它,然后在所有替换之后打印它

vows = "aeiouy"
myInput = raw_input("Insert a phrase containing vowels that you want to nullify: ")
lowerCased = myInput.lower()
for vow in vows:
  lowerCased = lowerCased.replace(vow, '0') # overwrites lowerCased with the updated replacement
print(lowerCased)
my_string.replace(“a”、“b”)
返回一个字符串,其中
a
b
替换。因此,您可以存储该值并每次替换它,然后在所有替换之后打印它

vows = "aeiouy"
myInput = raw_input("Insert a phrase containing vowels that you want to nullify: ")
lowerCased = myInput.lower()
for vow in vows:
  lowerCased = lowerCased.replace(vow, '0') # overwrites lowerCased with the updated replacement
print(lowerCased)
my_string.replace(“a”、“b”)
返回一个字符串,其中
a
b
替换。因此,您可以存储该值并每次替换它,然后在所有替换之后打印它

vows = "aeiouy"
myInput = raw_input("Insert a phrase containing vowels that you want to nullify: ")
lowerCased = myInput.lower()
for vow in vows:
  lowerCased = lowerCased.replace(vow, '0') # overwrites lowerCased with the updated replacement
print(lowerCased)

更换所有voyels后,必须在循环外部使用
print()

由于
replace()
返回一个字符串,只需更新
小写的
变量中的结果即可

lowerCased = myInput.lower()
for vow in vows:
#The 'if' is not necessary
    lowerCased = lowerCased.replace(vow, "0")
print lowerCased

更换所有voyels后,必须在循环外部使用
print()

由于
replace()
返回一个字符串,只需更新
小写的
变量中的结果即可

lowerCased = myInput.lower()
for vow in vows:
#The 'if' is not necessary
    lowerCased = lowerCased.replace(vow, "0")
print lowerCased

更换所有voyels后,必须在循环外部使用
print()

由于
replace()
返回一个字符串,只需更新
小写的
变量中的结果即可

lowerCased = myInput.lower()
for vow in vows:
#The 'if' is not necessary
    lowerCased = lowerCased.replace(vow, "0")
print lowerCased

更换所有voyels后,必须在循环外部使用
print()

由于
replace()
返回一个字符串,只需更新
小写的
变量中的结果即可

lowerCased = myInput.lower()
for vow in vows:
#The 'if' is not necessary
    lowerCased = lowerCased.replace(vow, "0")
print lowerCased

不断更新循环内的字符串,并将打印语句置于循环外:

vows = "aeiouy"
myInput = raw_input("Insert a phrase containing vowels that you want to nullify: ")
lowerCased = myInput.lower()
for vow in vows:
    if vow in lowerCased:
            lowerCased = lowerCased.replace(vow, "0")
print(lowerCased)
vows = "aeiouy"
myInput = raw_input("Insert a phrase containing vowels that you want to nullify: ")
lowerCased = myInput.lower()
for vow in vows:
    if vow in lowerCased:
            lowerCased = lowerCased.replace(vow, "0")
print(lowerCased)
或者,您可以使用这种紧凑的循环写入方式:

vows = "aeiouy"
myInput = raw_input("Insert a phrase containing vowels that you want to nullify: ")
lowerCased = myInput.lower()
for vow in vows:
    if vow in lowerCased:
            lowerCased = lowerCased.replace(vow, "0")
print(lowerCased)
vows = "aeiouy"
myInput = raw_input("Insert a phrase containing vowels that you want to nullify: ")
lowerCased = myInput.lower()
for vow in vows:
    if vow in lowerCased:
            lowerCased = lowerCased.replace(vow, "0")
print(lowerCased)

不断更新循环内的字符串,并将打印语句置于循环外:

vows = "aeiouy"
myInput = raw_input("Insert a phrase containing vowels that you want to nullify: ")
lowerCased = myInput.lower()
for vow in vows:
    if vow in lowerCased:
            lowerCased = lowerCased.replace(vow, "0")
print(lowerCased)
vows = "aeiouy"
myInput = raw_input("Insert a phrase containing vowels that you want to nullify: ")
lowerCased = myInput.lower()
for vow in vows:
    if vow in lowerCased:
            lowerCased = lowerCased.replace(vow, "0")
print(lowerCased)
或者,您可以使用这种紧凑的循环写入方式:

vows = "aeiouy"
myInput = raw_input("Insert a phrase containing vowels that you want to nullify: ")
lowerCased = myInput.lower()
for vow in vows:
    if vow in lowerCased:
            lowerCased = lowerCased.replace(vow, "0")
print(lowerCased)
vows = "aeiouy"
myInput = raw_input("Insert a phrase containing vowels that you want to nullify: ")
lowerCased = myInput.lower()
for vow in vows:
    if vow in lowerCased:
            lowerCased = lowerCased.replace(vow, "0")
print(lowerCased)

不断更新循环内的字符串,并将打印语句置于循环外:

vows = "aeiouy"
myInput = raw_input("Insert a phrase containing vowels that you want to nullify: ")
lowerCased = myInput.lower()
for vow in vows:
    if vow in lowerCased:
            lowerCased = lowerCased.replace(vow, "0")
print(lowerCased)
vows = "aeiouy"
myInput = raw_input("Insert a phrase containing vowels that you want to nullify: ")
lowerCased = myInput.lower()
for vow in vows:
    if vow in lowerCased:
            lowerCased = lowerCased.replace(vow, "0")
print(lowerCased)
或者,您可以使用这种紧凑的循环写入方式:

vows = "aeiouy"
myInput = raw_input("Insert a phrase containing vowels that you want to nullify: ")
lowerCased = myInput.lower()
for vow in vows:
    if vow in lowerCased:
            lowerCased = lowerCased.replace(vow, "0")
print(lowerCased)
vows = "aeiouy"
myInput = raw_input("Insert a phrase containing vowels that you want to nullify: ")
lowerCased = myInput.lower()
for vow in vows:
    if vow in lowerCased:
            lowerCased = lowerCased.replace(vow, "0")
print(lowerCased)

不断更新循环内的字符串,并将打印语句置于循环外:

vows = "aeiouy"
myInput = raw_input("Insert a phrase containing vowels that you want to nullify: ")
lowerCased = myInput.lower()
for vow in vows:
    if vow in lowerCased:
            lowerCased = lowerCased.replace(vow, "0")
print(lowerCased)
vows = "aeiouy"
myInput = raw_input("Insert a phrase containing vowels that you want to nullify: ")
lowerCased = myInput.lower()
for vow in vows:
    if vow in lowerCased:
            lowerCased = lowerCased.replace(vow, "0")
print(lowerCased)
或者,您可以使用这种紧凑的循环写入方式:

vows = "aeiouy"
myInput = raw_input("Insert a phrase containing vowels that you want to nullify: ")
lowerCased = myInput.lower()
for vow in vows:
    if vow in lowerCased:
            lowerCased = lowerCased.replace(vow, "0")
print(lowerCased)
vows = "aeiouy"
myInput = raw_input("Insert a phrase containing vowels that you want to nullify: ")
lowerCased = myInput.lower()
for vow in vows:
    if vow in lowerCased:
            lowerCased = lowerCased.replace(vow, "0")
print(lowerCased)
我可以用一个重的