英文:
Assignment Python course
问题
#4. 如果水果不在列表中,将水果添加到列表并打印修改后的列表。如果水果已存在,则打印“该水果已经存在于列表中”。
fruits = ['banana', 'orange', 'mango', 'lemon']
fruit_dennis = 'apple'
if fruit_dennis in fruits:
print('该水果已经存在于列表中')
else:
fruits.append(fruit_dennis)
print(fruits)
当我执行它时,它显示“None”。为什么会这样?
在没有条件的情况下打印它,它能正常工作。
英文:
I have the following assignment from my python course:
#4. If a fruit doesn't exist in the list add the fruit to the list and print the modified list. If the fruit exists print('That fruit already exist in the list')
fruits = ['banana', 'orange', 'mango', 'lemon']
fruit_dennis = 'apple'
if fruit_dennis in fruits : print ('That fruit already exist in the list')
else: print (fruits .append (fruit_dennis) )
When i excecute it says None. Why is that?
Printed without the condition and it worked.
答案1
得分: 1
fruits.append()
会就地修改列表并返回None
。你应该改成这样:
fruits = ['banana', 'orange', 'mango', 'lemon']
fruit_dennis = 'apple'
if fruit_dennis in fruits:
print('该水果已存在于列表中')
else:
fruits.append(fruit_dennis)
print(fruits)
英文:
fruits.append()
modifies the list in-place and returns None
. You want to do this instead:
fruits = ['banana', 'orange', 'mango', 'lemon']
fruit_dennis = 'apple'
if fruit_dennis in fruits:
print('That fruit already exist in the list')
else:
fruits.append(fruit_dennis)
print(fruits)
答案2
得分: 0
问题在于append
函数会在原地修改列表,而不返回任何内容。
你在打印时使用了(fruits.append(fruit_dennis))
,所以Python会在打印之前执行append
方法,append
方法会将fruit_dennis
正确地放入列表中,然后不返回任何内容供打印函数打印。
fruits = ['banana', 'orange', 'mango', 'lemon']
fruit_dennis = 'apple'
if fruit_dennis in fruits:
print('That fruit already exists in the list')
else:
fruits.append(fruit_dennis)
print(fruits)
这将是正确的。
英文:
The problem is that the append function modifies your list in-place, while returning nothing.
you are printing (fruits.append(fruit_dennis)), so python will execute the append method before printing it, the append method will correctly place fruit_dennis in the list, and then return nothing for the print function to print.
fruits = ['banana', 'orange', 'mango', 'lemon']
fruit_dennis = 'apple'
if fruit_dennis in fruits:
print('That fruit already exist in the list')
else:
fruits.append(fruit_dennis)
print(fruits)
this will be correct
答案3
得分: 0
如果逐行跟踪你的代码,你会发现"apple"被添加到列表中,但"append"返回None
,所以你打印的是它本身而不是列表。
要修复它,你可以编辑你的代码以打印更新后的列表,如下所示:
fruits = ['banana', 'orange', 'mango', 'lemon']
fruit_dennis = 'apple'
if fruit_dennis in fruits:
print('That fruit already exists in the list')
else:
fruits.append(fruit_dennis)
print(fruits)
这应该可以正常工作。
英文:
If you trace your code line by line, you will find apple gets appended to the list but append returns None
so you print that instead of the list itself.
To fix it you can edit your code to print the updated list like this:
fruits = ['banana', 'orange', 'mango', 'lemon']
fruit_dennis = 'apple'
if fruit_dennis in fruits:
print ('That fruit already exist in the list')
else:
fruits.append (fruit_dennis)
print (fruits)
this should work.
答案4
得分: -2
- 你可以使用三个反引号来表示代码,
像这样
- 在else条件中,你没有打印列表。
另外,顺便提一下:不建议要求别人解决你的作业。
英文:
- You can use triple backticks for code
looks like this
- You did not print the list in the else condition.
And also just a side note: It's really not recommended to ask people to solve your assignments.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论