简体   繁体   English

用户说出文字时如何打印?

[英]How to print something when a user gives a word?

My question can be understood below: 我的问题可以在下面理解:

goodvalue=False
while (goodvalue==False):
   try:
      word=str(input("Please enter a word: "))
   except ValueError:
       print ("Wrong Input...")
   else:
       goodvalue=True

word=word.lower()
List=list(map(str,word))
lenList=len(list(map(str,word)))
listofans=[]
x=0
while (lenList-1==x):
    if List[x]==str("a"):
        listofans[x]=str("1")
        x=x+1
    elif List[x]==str("b"):
        listofans[x]=str("2")
        x=x+1
    elif List[x]==str("c"):
        listofans[x]=str("3")
        x=x+1

It continues like that for all alphabets for a while... And then: 对于所有字母,它会持续一段时间……然后:

sumofnums=listofans[0]       
y=1
while (lenList-2==y):
    sumofnums+=listofans[y]

print ("The number is: ", sumofnums)

So basically, if I give hello, it should return 8 5 12 12 15. Any help is truly appreciated! 因此,基本上,如果我打招呼,它应该返回8 5 12 1215。真的感谢您的帮助!

Your code is very messy, and some of it isn't even needed (all those uses of map is not needed. Nor is the try/except structure) 您的代码非常混乱,甚至不需要使用其中的某些代码(不需要使用map所有内容。try try/except结构也不需要)

Why not simplify it a bit ;). 为什么不简化一下;)。

>>> import string
>>> d = {j:i for i, j in enumerate(string.lowercase, 1)}
>>> for i in 'hello':
...     print d[i],
... 
8 5 12 12 15

Some problems with your code: 您的代码存在一些问题:

  • Don't compare booleans like that . 不要像这样比较布尔值 Just do while goodvalue . 只要做while goodvalue

  • List=list(map(str,word)) is excessive. List=list(map(str,word))过多。 A simple List = list(word) is needed, but you probably won't even need this as you can iterate through strings (as I have shown above) 需要一个简单的List = list(word) ,但是您甚至可能不需要它,因为您可以遍历字符串(如上所示)

  • str("a") is pointless. str("a")是没有意义的。 "a" is already a string, thus str() does nothing here. "a"已经是一个字符串,因此str()在此不执行任何操作。

  • As I said before, the try/except is not needed. 正如我之前所说,不需要try/except No input could cause a ValueError . 没有输入可能导致ValueError (unless you meant int() ) (除非您的意思是int()

Are looking for something like this? 在寻找这样的东西吗?

[ord(letter)-ord('a')+1 for letter in word]

For "hello" this outputs: 对于“ hello”,输出:

[8, 5, 12, 12, 15]

The ord function returns the ascii ordinal value for the letter. ord函数返回字母的ascii序数值。 Subtracting ord('a') rebases that to 0, but you have 'a' mapping to 1, so it has to be adjusted by 1. 减去ord('a')会将其重新设置为0,但是您将'a'映射为1,因此必须将其调整为1。

First of all just for make your code smaller you have to look on a small stuff like, instead of print =="a" you print ==str("a") . 首先,为了使代码更小,您必须看一小段东西,而不是print =="a" ,而是打印==str("a") That's wrong. 错了

So here is your old while loop: 所以这是您的旧while循环:

while (lenList-1==x):
    if List[x]==str("a"):
        listofans[x]=str("1")
        x=x+1
    elif List[x]==str("b"):
        listofans[x]=str("2")
        x=x+1
    elif List[x]==str("c"):
        listofans[x]=str("3")
        x=x+1

And here is new: 这是新的:

while (lenList-1==x):
    if List[x]=="a":
        listofans[x]="1"
        x=x+1
    elif List[x]=="b":
        listofans[x]="2"
        x=x+1
    elif List[x]=="c":
        listofans[x]="3"
        x=x+1

And about your question, here is a solution: 关于您的问题,这是一个解决方案:

[ord(string)-ord('a')+1 for string in word]

If you print "hello" this will returns you: 如果打印“ hello”,将返回您:

[8, 5, 12, 12, 15]

Try this: 尝试这个:

goodvalue=False
while (goodvalue==False):
   try:
      word=str(input("Please enter a word: "))
   except ValueError:
       print ("Wrong Input...")
   else:
       goodvalue=True

word=word.lower()
wordtofans=[]

for c in word:
    if c >= 'a' and c <= 'z':
        wordtofans.append( int(ord(c)-ord('a')+1) )

print wordtofans

You can directly iterate a string in a for loop, you don't have to convert your string to a list. 您可以直接在for循环中迭代字符串,而不必将字符串转换为列表。

You have a control check here to ensure that only letters a..z and A..Z are converted into numbers. 您在此处进行了控件检查,以确保仅将字母a..z和A..Z转换为数字。

Conversion from a string letter to a number is done using int(ord(c)-ord('a')+1) which uses ord function that will return a ASCII value for a supplied character. 从字符串字母到数字的转换是使用int(ord(c)-ord('a')+1) ,该函数使用ord函数,该函数将为提供的字符返回ASCII值。

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

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