python 21点游戏,似乎忽略了';如果';陈述
我一直在尝试用python制作一个简单的21点游戏,我似乎被卡住了,我的代码如下:python 21点游戏,似乎忽略了';如果';陈述,python,if-statement,python-2.7,Python,If Statement,Python 2.7,我一直在尝试用python制作一个简单的21点游戏,我似乎被卡住了,我的代码如下: 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:
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
来自随机导入选择
def deck():
卡片=范围(1,12)
返回选择(卡片)
def diack():
card1=甲板()
card2=甲板()
手动=卡1+卡2
印刷手
如果手<21:
打印原始输入(“您想击球还是站立?”)
如果“命中”:
返回手+甲板()
以利夫的“立场”:
回手
当我运行时,它似乎对“hit”有效,但当我输入“stand”时,它似乎也“hit”。正如你现在可能知道的,我对编程非常陌生。你们能帮我指出如何让我的游戏正常运行的正确方向吗(我希望尽可能多地使用我的代码) 假设缩进正确:
print raw_input("Would you like to hit or stand?")
if "hit":
return hand + deck()
elif "stand":
return hand
您的if
只是检查字符串“hit”
是否为真。所有非空字符串都为true,“hit”
为非空,因此此操作将始终成功
你想要的是这样的:
cmd = raw_input("Would you like to hit or stand?")
if cmd == "hit":
return hand + deck()
elif cmd == "stand":
return hand
现在,您正在检查
原始输入的结果是否是您想要的字符串“hit”
。假设缩进正确:
print raw_input("Would you like to hit or stand?")
if "hit":
return hand + deck()
elif "stand":
return hand
您的if
只是检查字符串“hit”
是否为真。所有非空字符串都为true,“hit”
为非空,因此此操作将始终成功
你想要的是这样的:
cmd = raw_input("Would you like to hit or stand?")
if cmd == "hit":
return hand + deck()
elif cmd == "stand":
return hand
现在,您正在检查原始输入的结果是否是您想要的字符串“hit”
。如果“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
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
这只会给您一个缩进错误,因为第二个if
缩进在print
下,这不是控制语句。请发布您的实际代码。这只会给您一个缩进错误,因为第二个if
缩进在print
下,这不是控制语句。请张贴您的实际代码。非常感谢!我明白我做错了什么,我真的很感激。非常感谢!我明白我做错了什么,我真的很感激。谢谢!它现在似乎起作用了。另一个注意事项是,在维护缩进的同时,他们可以将代码复制并粘贴到stackoverflow中?我只是从emacs中复制,粘贴到窗口中,然后单击{}
按钮缩进所选的4个空格。谢谢!它现在似乎起作用了。另一个注意事项是,它们是一种在维护缩进的同时将代码复制并粘贴到stackoverflow中的方法吗?我只是从emacs中复制,粘贴到窗口中,然后单击{}
按钮将所选内容缩进4个空格。