python 如何制作一个程序,告诉用户他们的姓或名中有多少个字母

xkftehaa  于 2023-06-20  发布在  Python
关注(0)|答案(3)|浏览(102)

如何制作一个程序,告诉用户他们的姓或名中有多少个字母。这里我有代码,所以也许你有任何调整,只是让我知道。Thanks:)

vdgimpew

vdgimpew1#

print(len(input(“What is your name?“)))

yqkkidmi

yqkkidmi2#

print(len(input(“What is your name?“)))

pn9klfpd

pn9klfpd3#

不幸的是,您的代码是在图像中。我使用screen reader,所以看不到图像,但我可以编写一个示例程序:

#Example 1, count all characters in the user's input
fn=input("What is your first name?")
ln=input("What is your surname?")
print("Your first name has " + str(len(fn)) + " characters, and your surname has " + str(len(ln)) + " characters.")

#Example 2, count *only* ASCII letters in user's input
import string
fn=input("What is your first name?")
ln=input("What is your surname?")
fncount=0
lncount=0
for i in fn:
    if i in string.ascii_letters:
        fncount+=1
for i in ln:
    if i in string.ascii_letters:
        lncount+=1
print("Your first name has " + str(fncount) + " letters, and your surname has " + str(lncount) + " letters.")

相关问题