为什么这些变量不起作用?

时间:2014-10-24 11:37:57

标签: python function variables

我是编程的真正新手。我一直在经历“以艰难的方式学习Python”,这真的很棒。我现在已经走了,试着在我自己制作一个类似于19练习中的小脚本,只是为了练习。练习运行函数的不同方法。

以下变量似乎不起作用:

beerseach = totalbeers / guests
spiritseach = totalspirits / guests

party(beerseach, spiritseach)

我收到错误“不支持的操作数类型:'int'和'str'”。

如果问题出在上下文中,我已将下面的其余代码放在下面。

def party(beer, spirits):
    print "We have %s beers" % beer
    print "We have %s bottles of spirits" % spirits
    print "Is it enough? We'll find out tonight\n"

beers = 450
spiritbottles = 40

totalbeers = 546 + 124
totalspirits = beers / spiritbottles

beeramount = raw_input("How many beers are you bringing? ")
spiritamount = raw_input("How many spirits are you bringing? ")
#guests = raw_input("How many guests are coming? ")

party(500, 50)

party(beers, spiritbottles)

party(totalbeers, totalspirits)

party(beeramount, spiritamount)

party(beers + 49, spiritbottles - 21)

#party(guests + beers, guests + totalspirits) apparently cannot do this with int objects/variables/whatever

print 20 * 450
print beeramount * totalspirits

raw_input("Now we move on. Press enter.")

guests = raw_input("How many guests are coming? ")
print "We're going to have to work out how many beers each person can have"

beerseach = totalbeers / guests
spiritseach = totalspirits / guests
                                    #WHY DOESN'T THIS WORK????!
party(beerseach, spiritseach)

1 个答案:

答案 0 :(得分:5)

您将guests变量视为

guests = raw_input("How many guests are coming? ")

raw_input的回复是str。您必须将其转换为int

guests = int(raw_input("How many guests are coming? "))