def hit():
global hitsum
hitsum = 0
v=random.choice(cards)
c=random.choice(suits)
if v=="Ace":
hitsum=hitsum+1
print "You were dealt","a",v,"of",c
elif v=="Jack":
hitsum=hitsum+11
print "You were dealt","a",v,"of",c
elif v=="Queen":
hitsum=hitsum+12
print "You were dealt","a",v,"of",c
elif v=="King":
hitsum=hitsum+13
print "You were dealt","a",v,"of",c
else:
hitsum=hitsum+v
print "You were dealt","a",v,"of",c
computer()
choice=raw_input("Would you like to hit or stay? ")
if choice=="hit":
hit()
totalsum = hitsum + usersum
print "Your total is", totalsum
elif choice=="stay":
totalsum=usersum
else:
print "Invalid request"
这段代码是我21点游戏的摘录。我做了一个用户定义的函数,每当有人要求点击时随机生成一张卡。然而,这只适用于一种选择。如果我选择命中一次,我就不能再选择了。我该如何纠正?你知道吗
我强烈建议更改
hit
和hitsum
的处理方式。与其让它全球化,为什么不退货呢?所以在hit
的结尾,你会所以在电话里你可以
我也看到了其他一些问题。下一次通过
choice==hit
循环时,usersum将恢复到以前的状态。我不认为那是你想要的。当然你想把usersum
增加hitsum
。在这种情况下,将totalsum = ...
替换为最后,在
hit
函数中,为什么要在开头定义hitsum=0
?你知道吗相关问题 更多 >
编程相关推荐