英文:
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:
def recursion(user):
if len(user) == 0:
return ""
else:
return user[0].upper() + recursion(user[1:])
#user input
user = input("Please input your array: ")
print("Output", recursion(user))
[here is the output]
Please input your array: hi
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:
def recursion(user):
if len(user) == 0:
return []
else:
return [user[0].upper()] + recursion(user[1:])
#user input
user = input("Please input your array: ")
print("Output" , ((recursion(user))))
[here is the output]
Please input your array: hi
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
你的递归函数返回列表。如果你想将其保留为字符串,可以使用以下代码:
def recursion(user):
if len(user) == 0:
return ""
else:
return user[0].upper() + recursion(user[1:])
# 用户输入
user = input("请输入你的数组:")
print("输出", ((recursion(user))))
英文:
You are returning lists in your recursive function. If you want to keep it as a string, you can use:
def recursion(user):
if len(user) == 0:
return ""
else:
return user[0].upper() + recursion(user[1:])
#user input
user = input("Please input your array: ")
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:
recursion(["Hi"])
The output is:
['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:
recursion(["Hi"])
The output is:
['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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论