iam*_*sla 1 python if-statement python-2.7
我一直在尝试在python中制作一个简单的二十一点游戏,我似乎卡住了,我的代码如下:
from random import choice
def deck():
cards = range(1, 12)
return choice(cards)
def diack():
card1= deck()
card2 = deck()
hand = card1 + card2
print hand
if hand < 21:
print raw_input("Would you like to hit or stand?")
if "hit":
return hand + deck()
elif "stand":
return hand
Run Code Online (Sandbox Code Playgroud)
当我运行它似乎工作"击中",但当我键入"立场"它似乎"击中"以及.正如你现在可能知道的那样,我对编程非常陌生.你们能帮助我指出如何使我的游戏工作正确的方向(我想尽可能多地使用我的代码).
if "hit"只是测试字符串是否"hit"存在,它确实存在.因此,该elif语句永远不会执行.
您需要捕获变量中的用户输入并对其进行测试:
choice = raw_input("Would you like to hit or stand?")
print choice
if choice == "hit":
return hand + deck()
elif choice == "stand":
return hand
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
438 次 |
| 最近记录: |