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"
此代碼是從我的二十一點遊戲摘錄。我做了一個用戶定義的函數,用於在有人要求命中時隨機生成一張卡片。但是,這隻適用於一種選擇。如果我選擇一次,我沒有選擇再次選擇它。我該如何糾正?如何在Python中多次使用'if'循環?
你需要一個while循環或for循環使用範圍 – 2015-02-07 10:16:29