How to use an 'if' loop multiple times in python?
本问题已经有最佳答案,请猛点这里访问。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 | 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" |
这段代码摘自我的二十一点游戏。 我创建了一个用户定义的函数,用于在有人要求命中时随机生成一张卡片。 然而,这仅适用于一种选择。 如果我选择点击一次,我不会再选择它。 我该如何纠正?
1 2 3 4 5 6 | choice=raw_input("Would you like to hit or stay?") while choice=="hit": hit() totalsum = hitsum + usersum print"Your total is", totalsum choice=raw_input("Would you like to hit or stay?") |
我强烈建议更改
1 | return hitsum |
那么你可以在通话中做到
1 | totalsum = usersum + hit() |
我在这里也看到了一些其他问题。 下次通过
1 | usersum += hit() |
最后,在