Ben*_*nno -2 python variables function
我是编程的真正新手.我一直在经历"以艰难的方式学习Python",这真的很棒.我现在已经走了,试着在我自己制作一个类似于19练习中的小脚本,只是为了练习.练习不同的运行方式.
以下变量似乎不起作用:
beerseach = totalbeers / guests
spiritseach = totalspirits / guests
party(beerseach, spiritseach)
Run Code Online (Sandbox Code Playgroud)
我得到错误"不支持的操作数类型:'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)
Run Code Online (Sandbox Code Playgroud)
你得到guests变量为
guests = raw_input("How many guests are coming? ")
Run Code Online (Sandbox Code Playgroud)
归来raw_input是一个str.你必须将它转换为int
guests = int(raw_input("How many guests are coming? "))
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
53 次 |
| 最近记录: |