为什么在使用pop()方法时变量没有更新?

时间:2019-08-07 12:06:40

标签: python

我正在通过Python速成课程学习Python,并且在列表一章中。

我有一个来宾列表,我应该为每个列表打印一条消息(尚未循环),在其中我必须告诉他们不使用pop()方法邀请他们参加活动。

#I tried this :

removed_guest = guestlist.pop()
apology = 'you cannot come to dinner ' + removed_guest
print(apology)
print(apology)
print(apology)


#but I figured the variable needs to be updated for each print

#I tried re-adding the removed_guest variable for each print:


removed_guest = guestlist.pop()
apology = 'you cannot come to dinner ' + removed_guest
print(apology)
removed_guest = guestlist.pop()
print(apology)
removed_guest = guestlist.pop()
print(apology)


#but this is where I made a mistake because the same thing happens (shouldn't removed guest list update with the newest last item in the list since I already popped that one out?

#at last I did this:

apology = 'you cannot come to dinner '
print(apology + guestlist.pop())
print(apology + guestlist.pop())
print(apology + guestlist.pop())

#this works but I'm curious as to why the apology variable doesn't get updated even though I updated remove_guest.

7 个答案:

答案 0 :(得分:2)

赋值语句( x = y )在执行时向 x 赋值;他们没有定义某种导致重复该动作的关系。

答案 1 :(得分:1)

  

这有效,但是对于为什么道歉变量即使在更新了remove_guest之后也没有得到更新却感到好奇

我相信你的意思在这里?

removed_guest = guestlist.pop()
apology = 'you cannot come to dinner ' + removed_guest
print(apology)
removed_guest = guestlist.pop()
print(apology)
removed_guest = guestlist.pop()
print(apology)

让我们说我们有客人A,B和C。 因此您的代码将像这样。在第一行中,remove_guest将设置为C。然后,您会说道歉是“您不能进餐'+'C'。然后您将打印出来。然后,您会说将remove_guest设置为B。然后,您将打印道歉。然后,您将说被删除的客人是A,然后您将道歉。但是,正如您所看到的,道歉只与来宾C一起设置过一次。但是道歉的内容始终是“您不能来晚餐” +“ C”。

答案 2 :(得分:1)

如果我清楚地了解您的查询,则您只会在每个打印命令之前更新removed_guest变量(而不是道歉变量),但希望道歉也会得到更新。

为了更新道歉,您需要在每个打印命令之前对其进行更新:

removed_guest = guestlist.pop()
apology = 'you cannot come to dinner ' + removed_guest
print(apology)
removed_guest = guestlist.pop()
apology = 'you cannot come to dinner ' + removed_guest
print(apology)
removed_guest = guestlist.pop()
apology = 'you cannot come to dinner ' + removed_guest
print(apology)

答案 3 :(得分:1)

访问变量将为您提供存储在其中的先前值。

访问变量不会重新运行用于定义变量的表达式。这将无视变量的要点,即记住一个值而不必重新计算它。

您可能尚未了解功能;函数就是您要考虑的:一种按需重新运行相同表达式的方法。

guestlist = ['Foo', 'Bar', 'Baz']

def apologize():
    return 'you cannot come to dinner ' + guestlist.pop()

print(apologize()) # you cannot come to dinner Baz
print(apologize()) # you cannot come to dinner Bar
print(apologize()) # you cannot come to dinner Foo

答案 4 :(得分:1)

在此代码块中,您为removed_guest变量分配了特定的removed_guest值。变量不是指针。当您分配一个变量时,您会将名称绑定到一个对象。从那时起,您可以使用名称来引用对象,直到该名称被反弹为止。定义字符串时,它将创建对内存中特定字符串值的引用。

removed_guest = guestlist.pop()
#String is defined and saved in the memory 
apology = 'you cannot come to dinner ' + removed_guest 
print(apology)
removed_guest = guestlist.pop()
print(apology)
removed_guest = guestlist.pop()
print(apology)

答案 5 :(得分:1)

您不会在每个apology之后更新pop。可变的歉意分配是通过值而不是removed_guest的引用来进行的。因此,即使更改apology,变量removed_guest也不会自行更新。 您可以在每次弹出后打印之前进行apology = 'you cannot come to dinner ' + removed_guest
如果要使用功能,请尝试以下操作:

def apology(guest):
    return 'you cannot come to dinner ' + removed_guest

removed_guest = guestlist.pop()
print(apology(removed_guest))
removed_guest = guestlist.pop()
print(apology(removed_guest))
removed_guest = guestlist.pop()
print(apology(removed_guest))

答案 6 :(得分:-1)

因为它是一个字符串,所以如果希望它返回参数特定值,则应该这样做。

def apology(guest_name):
    return "you cannot come to dinner "+str(guest_name)

guestlist=["abc","def","jkl"]
print(apology(guestlist.pop()))
print(apology(guestlist.pop()))
print(apology(guestlist.pop()))