python二十一点游戏,似乎忽略了'如果'的说法 [英] python blackjack game, seems to be ignoring 'if' statement

查看:128
本文介绍了python二十一点游戏,似乎忽略了'如果'的说法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在尝试在python中制作一个简单的二十一点游戏,我似乎卡住了,我的代码如下:

I've been trying to make a simple blackjack game in python and I seem to be stuck, my code is as follows:

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

当我运行时,它似乎适用于点击但是当我输入时站立它似乎也打了。正如你现在可能知道的那样,我对编程非常陌生。你们能帮助我指出如何使我的游戏工作正确的方向(我想尽可能多地使用我的代码)。

When I run that it seems to work for "hit" but when I type in "stand" it seems to "hit" aswell. As you can probably tell by now I am extremely new to programming. Could you guys help point me in the right direction on how to make my game work (I'd like to use as much of my code as possible).

推荐答案

如果点击只测试字符串hit存在,确实如此。因此, elif 语句永远不会执行。

if "hit" just tests if the string "hit" exists, and it does. Thus, the elif statement is never executed.

您需要捕获变量中的用户输入并进行测试相反:

You need to capture the user input in a variable and test against that instead:

choice = raw_input("Would you like to hit or stand?")
print choice
if choice == "hit":
    return hand + deck()
elif choice == "stand": 
    return hand

这篇关于python二十一点游戏,似乎忽略了'如果'的说法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆