2

私はプログラミングが初めてで、Python を学ぶための無料のオンライン チュートリアルについて助けが必要です。入力文字列をすべて小文字に変換する独自の方法を構築しています。string.lower() メソッドを使用できません。これまでのコードでは、入力文字列を文字コンバーターlowerChar(char)に入力できる文字に分割する方法がわかりません。

string=input #input string


def lowerChar(char):              #function for converting characters into lowercase
   if ord(char) >= ord('A') and ord(char)<=ord('Z'):
      return chr(ord(char) + 32)
   else:
      return char

def lowerString(string):     #function for feeding characters of string into lowerChar
   result = ""
  for i in string:
     result = result + lowerChar(string[i])
     return result
4

5 に答える 5

3

あなたは本当に近いです:

def lowerString(string):
  result = ""
  for i in string:
     # i is a character in the string
     result = result + lowerChar(i)
  # This shouldn't be under the for loop
  return result

文字列はリストと同じように反復可能です!

また、インデント レベルに注意し、使用するスペースの数は一定にする必要があります。

于 2013-08-22T16:48:57.497 に答える
1

最初の文字のみを返していますreturn。外側のスコープでこれを試してください。また、+=の代わりに使用することをお勧めしますresult = result + lowerChar(i)

def lowerString(string):     #function for feeding characters of string into lowerChar
  result = ""
  for i in string:
     result  += lowerChar(i) 
  return result

print lowerString("HELLO") #hello
于 2013-08-22T16:53:24.420 に答える
0

私の解決策:

string = input("Input one liner: ")

def lowerChar(char):
    if char >= 65 and char <= 90:
        char = chr(char + 32)
        return char
    else:
        char = chr(char)
        return char


def lowerString(string):
    result = ""
    for i in range(0, len(string)):
        result = result + lowerChar(ord(string[i]))
    return result

print(lowerString(string))
于 2016-09-24T16:10:12.663 に答える