如何将输出更改为字符串而不是输出列表?

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

How do I change the output to a string instead of it outputting a list?

问题

I'm learning python and cannot get the output to be a string instead of a list. Can someone point me in the right direction please?

Here is my code:

  1. def recursion(user):
  2. if len(user) == 0:
  3. return ""
  4. else:
  5. return user[0].upper() + recursion(user[1:])
  6. #user input
  7. user = input("Please input your array: ")
  8. print("Output", recursion(user))

[here is the output]

  1. Please input your array: hi
  2. Output HI

Process finished with exit code 0

The outcome I was trying for was HI.

英文:

I'm learning python and cannot get the output to be a string instead of a list. Can someone point me in the right direction please?

Here is my code:

  1. def recursion(user):
  2. if len(user) == 0:
  3. return []
  4. else:
  5. return [user[0].upper()] + recursion(user[1:])
  6. #user input
  7. user = input("Please input your array: ")
  8. print("Output" , ((recursion(user))))

[here is the output]

  1. Please input your array: hi
  2. Output ['H', 'I']

Process finished with exit code 0

https://i.stack.imgur.com/7diMa.png

The outcome I was trying for was [HI]

答案1

得分: 1

你的递归函数返回列表。如果你想将其保留为字符串,可以使用以下代码:

  1. def recursion(user):
  2. if len(user) == 0:
  3. return ""
  4. else:
  5. return user[0].upper() + recursion(user[1:])
  6. # 用户输入
  7. user = input("请输入你的数组:")
  8. print("输出", ((recursion(user))))
英文:

You are returning lists in your recursive function. If you want to keep it as a string, you can use:

  1. def recursion(user):
  2. if len(user) == 0:
  3. return ""
  4. else:
  5. return user[0].upper() + recursion(user[1:])
  6. #user input
  7. user = input("Please input your array: ")
  8. print("Output" , ((recursion(user))))

答案2

得分: 0

Your function is correct but you have a small misunderstanding of the assignment.

The assignment doesn't require any console input or output (print). The wording of the assignment only asks for a function. The function is the first 5 lines of your programs and it does it exactly what it should do. To test your function, simply write:

  1. recursion(["Hi"])

The output is:

  1. ['HI']

I leave it to you to try with an array of multiple words.

The reason that input doesn't work as you expected is the following: input returns a string. That string is an array (!)* of characters. But characters are still strings (with length 1). If you take the assignment literally, you don't have to worry about that special case.

*The equivalent to an "array" in other languages is called a "sequence" in Python.

英文:

Your function is correct but you have a small misunderstanding of the assignment.

The assignment doesn't require any console input or output (print). The wording of the assignment only asks for a function. The function is the first 5 lines of your programs and it does it exactly what it should do. To test your function, simply write:

  1. recursion(["Hi"])

The output is:

  1. ['HI']

I leave it to you to try with an array of multiple words.

The reason that input doesn't work as you expected is the following: input returns a string. That string is an array (!)* of characters But characters are still strings (with length 1). If you take the assignment literally, you don't have to worry about that special case.

*The equivalent to an "array" in other languages is called a "sequence" in Python.

huangapple
  • 本文由 发表于 2023年4月17日 06:02:36
  • 转载请务必保留本文链接:https://go.coder-hub.com/76030540.html
匿名

发表评论

匿名网友

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

确定