将大写字母转换为它们对应的Unicode值?

huangapple go评论59阅读模式
英文:

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(&quot;Enter your message: &quot;)

#convert string to unicode
#print resultant number
print(&quot;Your scrambled message is: &quot;, end=&#39;&#39;)

for char in message:
    if (96 &gt;= ord(char) &lt;= 122):
        print(char, end=&#39;&#39;)
    elif (65 &gt;= ord(char) &lt;= 90):
        print(char, end=&#39;&#39;)
    else:
       print(ord(char), end=&#39;&#39;)

print()

#allow user to unscramble
while True:
    response = input(&quot;Would you like to unscramble the message? (y/n): &quot;)

#print original message
    if response == &#39;y&#39;:
        print(&quot;Unscrambled&quot;)
        print(message)
        break
    elif response == &#39;n&#39;:
        print(&quot;Ok! Keep your secrets&quot;)
        break
    else:
        print(&quot;Unknown error!&quot;)

答案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=&#39;&#39;)
    if (65 &lt;= ord(char) &lt;= 90):
        print(ord(char), end=&#39;&#39;)
    elif (96 &lt;= ord(char) &lt;= 122):
        print(ord(char), end=&#39;&#39;)
    else:
        print(char, end=&#39;&#39;)

答案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(&quot;Enter your message: &quot;)

#convert string to unicode
#print resultant number
print(&quot;Your scrambled message is: &quot;, end=&#39;&#39;)

for char in message:
    if (96 &lt;= ord(char) &lt;= 122):
        print(char, end=&#39;&#39;)
    elif (65 &lt;= ord(char) &lt;= 90):
        print(char, end=&#39;&#39;)
    else:
       print(ord(char), end=&#39;&#39;)

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 &gt;= ord(char)) and (ord(char) &lt;= 122):
      print(ord(char), end=&#39;&#39;)
   elif (65 &gt;= ord(char)) and (ord(char) &lt;= 90):
      print(ord(char), end=&#39;&#39;)
   else:
      print(ord(char), end=&#39;&#39;)

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=&#39;&#39;)
   elif ord(char) in range(65,91) &lt;= 90):
      print(ord(char), end=&#39;&#39;)
   else:
      print(ord(char), end=&#39;&#39;)

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=&#39;&#39;)
   elif o in range(65,91) &lt;= 90):
      print(o, end=&#39;&#39;)
   else:
      print(o, end=&#39;&#39;)

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=&#39;&#39;)

huangapple
  • 本文由 发表于 2023年6月8日 19:17:48
  • 转载请务必保留本文链接:https://go.coder-hub.com/76431289.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定