英文:
How do I convert Uppercase letters to their correspondent Unicode value?
问题
我是一个新手程序员,尝试创建一个简单的Python程序,将字母转换为它们对应的Unicode值。该程序适用于小写字母,但对于大写字母没有任何作用。
以下是代码:
# 要求用户输入一个字符串
message = input("输入您的消息:")
# 将字符串转换为Unicode
# 打印结果数字
print("您的加密消息是:", end='')
for char in message:
if (96 >= ord(char) <= 122):
print(char, end='')
elif (65 >= ord(char) <= 90):
print(char, end='')
else:
print(ord(char), end='')
print()
# 允许用户解密
while True:
response = input("您是否要解密消息?(y/n):")
# 打印原始消息
if response == 'y':
print("已解密")
print(message)
break
elif response == 'n':
print("好的!保守您的秘密")
break
else:
print("未知错误!")
希望这有所帮助。
英文:
I am a noob programmer trying to create a simple python program that converts letters to their correspondent Unicode values. The program works for lowercase letters, but does not do anything for uppercase letters.
Here is the code
#ask user to enter a string
message = input("Enter your message: ")
#convert string to unicode
#print resultant number
print("Your scrambled message is: ", end='')
for char in message:
if (96 >= ord(char) <= 122):
print(char, end='')
elif (65 >= ord(char) <= 90):
print(char, end='')
else:
print(ord(char), end='')
print()
#allow user to unscramble
while True:
response = input("Would you like to unscramble the message? (y/n): ")
#print original message
if response == 'y':
print("Unscrambled")
print(message)
break
elif response == 'n':
print("Ok! Keep your secrets")
break
else:
print("Unknown error!")
答案1
得分: 2
在if条件中,你的符号写错了,我认为你想要在条件满足时打印 ord(char)
。
所以:
for char in message:
#print(char, ord(char), end='')
if (65 <= ord(char) <= 90):
print(ord(char), end='')
elif (96 <= ord(char) <= 122):
print(ord(char), end='')
else:
print(char, end='')
英文:
You had the signs wrong in the if conditions, and I think you want to print ord(char)
if the conditions are met.
So:
for char in message:
#print(char, ord(char), end='')
if (65 <= ord(char) <= 90):
print(ord(char), end='')
elif (96 <= ord(char) <= 122):
print(ord(char), end='')
else:
print(char, end='')
答案2
得分: 1
message = input("输入您的消息:")
# 将字符串转换为Unicode
# 打印结果数字
print("您的混淆消息是:", end='')
for char in message:
if (96 <= ord(char) <= 122):
print(char, end='')
elif (65 <= ord(char) <= 90):
print(char, end='')
else:
print(ord(char), end='')
print()
You've confused the sign in condition
英文:
message = input("Enter your message: ")
#convert string to unicode
#print resultant number
print("Your scrambled message is: ", end='')
for char in message:
if (96 <= ord(char) <= 122):
print(char, end='')
elif (65 <= ord(char) <= 90):
print(char, end='')
else:
print(ord(char), end='')
print()
You've confused the sign in condition
答案3
得分: 1
你在使用if语句和打印部分犯了一个错误。
for char in message:
if (96 >= ord(char)) and (ord(char) <= 122):
print(ord(char), end='')
elif (65 >= ord(char)) and (ord(char) <= 90):
print(ord(char), end='')
else:
print(ord(char), end='')
将来,你也可以使用range(start,end)
来代替这样复杂的if
语句。
for char in message:
if ord(char) in range(96,123):
print(ord(char), end='')
elif ord(char) in range(65,91):
print(ord(char), end='')
else:
print(ord(char), end='')
而不是多次调用ord()
,你可以使用一个变量来代替。
for char in message:
o = ord(char)
if o in range(96,123):
print(o, end='')
elif o in range(65,91):
print(o, end='')
else:
print(o, end='')
以上的程序会解决你的问题,但更高效的方式是这样,因为在你的程序中并没有太多使用if条件。
for char in message:
print(ord(char), end='')
英文:
You have done mistake in using your if statement and printing
for char in message:
if (96 >= ord(char)) and (ord(char) <= 122):
print(ord(char), end='')
elif (65 >= ord(char)) and (ord(char) <= 90):
print(ord(char), end='')
else:
print(ord(char), end='')
In future you can also use range(start,end)
instead of using such complex if
statement,
for char in message:
if ord(char) in range(96,123):
print(ord(char), end='')
elif ord(char) in range(65,91) <= 90):
print(ord(char), end='')
else:
print(ord(char), end='')
Instead of calling ord()
many times you can use a variable instead,
for char in message:
o=ord(char)
if o in range(96,123):
print(o, end='')
elif o in range(65,91) <= 90):
print(o, end='')
else:
print(o, end='')
Above program will solve your problem but efficient way to do your task would be simple as this as there is not much use of if condition in your program.
for char in message:
print(ord(char), end='')
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论