基本上对于这个赋值,我需要将一个名字转换成一个“秘密号码”,方法是将我给每个字母赋值的值相加。到目前为止一切正常,但当我试图总结它给我一个错误。我想我需要把这些字母转换成数字形式,但到目前为止我所做的一切都行不通。你知道吗
例如
total_name =0 for c in end_name
c_int = int(c)
total_name+= c_int
任何帮助都将不胜感激!这是我的密码:
'a' ==1
'b'==2
'c'==3
'd'==4
'e'==5
'f'==6
'g'==7
'h'==8
'i'==9
'j'==10
'k'==11
'l'==12
'm'==13
'n'==14
'o'==15
'p'==16
'q'==17
'r'==18
's'==19
't'==10
'u'==21
'v'==22
'w'==23
'x'==24
'y'==25
'z'==26
#input:ask user to enter their name
#processing: convert name into all lower case then calculate number
#output: return name and reduction to user
name = input('Name:')
new_name= str.lower(name)
end_name=new_name.replace(" ","")
print('Your "cleaned up" name is:',end_name)
total_name =0
for c in end_name
total_name+= c
print('Reduction:',total_name)
这里有一种方法:
对于输入
Jason
,我得到值59
将需要
ord()
函数将字符转换为数字。你知道吗‘A’的ASCII代码是65,因此可以减去64以缩放到首选偏移量。你知道吗
对于小写字母,“a”是ASCII代码97,但是任何大写字母都会得到负数。您也可以在循环之前将整个字符串规范化为大写(或小写,由您选择)。你知道吗
你少了一个冒号:
for c in end_name:
相关问题 更多 >
编程相关推荐