Python:高低游戏v2

2024-10-02 12:22:56 发布

您现在位置:Python中文网/ 问答频道 /正文

好吧,我基本上已经创建了它,但是仍然有一些问题。在

[更新]我已经根据信息更新了,我还有一些问题。在

import random

print '|'*20
print '='*20
print 'Instruction'
print 'Enter high, low or quit!'
print 'Press enter without any input too see instruction'
print '='*20
print "="*5+'GAME START'+"="*5
print '='*20
print ''
card = [2,3,4,5,6,7,8,9,10]

choice = ' '
i=0
first = 1
while choice != 'quit':


    card1, card2 = random.sample(card, 2)


    showCard = [card1, card2]
    startCard = card1

    if choice == 'high':
        if card1 > card2:
            print ''
            print str(card1) + ' first card1'
            print str(card2) + ' first card2'
            print ''
            print 'wrong'
            print ''
            print 'Dealer Second card: '+str(card2)
            print '-'*20
            i=i+1
        elif card1 <= card2:
            print ''
            print str(card1) + ' second card1'
            print str(card2) + ' second card2'
            print ''
            print 'correct'
            print ''
            print 'Dealer Second card: '+str(card2)
            print '-'*20
            i=i+1
        else:
            print 'Dealer First card: '+str(card1)
    elif choice == 'low':
        if card1 > card2:
            print 'wrong - testing 3'
            print 'Next card: '+str(card2)
            print '-'*20
        else:
            print 'correct - testing 4'
            print 'Next card: '+str(card2)
            print '-'*20
    elif choice != 'high' or 'low' or 'info':
        print 'Enter high or low only'


    print 'First card 2: '+str(startCard)

    choice = raw_input("Enter: ")

print i
print 'END'

输出:

^{pr2}$

问题:在第二个循环中,如何在“Enter:high”用户输入之前设置“6 first card1”?(将“第一张卡片2:3”替换为“6张第一张卡片1”)

PS:由于低输入仍在构建中,因此首先使用高输入

谢谢


Tags: orifrandomcardlowfirstprintenter
1条回答
网友
1楼 · 发布于 2024-10-02 12:22:56

您可以(如果可能的话)像taronish4所说的那样将声明移到外部,或者更改代码,使其看起来像这样:

choice = raw_input("Press enter to start ")
i=-1
first=1
while choice != 'quit':

    card = [2,3,4,5,6,7,8,9,10]
    card1, card2 = random.sample(card, 2)

    if first == 1:
        print 'Dealer First card: '+str(card1)
        first = 0

    showCard = [card1, card2]

    if choice == 'high':

代码的其余部分和以前一样。在

编辑:附加问题的解决方案

你必须复制这个:

^{pr2}$

把它放在循环之前,像这样:

card = [2,3,4,5,6,7,8,9,10]
card1, card2 = random.sample(card, 2)
choice = ' '
i=0
while choice != 'quit':

然后移动随机抽样在循环内部,以便在结束时,在输出之前执行,如下所示:

print 'First card 2: '+str(startCard)
card1, card2 = random.sample(card, 2)
choice = raw_input("Enter: ")

我建议删除第一个=1声明,因为您没有使用这个变量。在

编辑:JQKA问题

为此,我将实现一个字典,它返回数字10、11、12、13的字符串。 把它放在代码之前(但是在import语句之后!)在

cardDict = {
    2: '2',
    3: '3',
    4: '4',
    5: '5',
    6: '6',
    7: '7',
    8: '8',
    9: '9',
    10: '10',
    11: 'J',
    12: 'Q',
    13: 'K',
    14: 'A'
}

接下来就是剩下的代码了。 别忘了修改您的名片列表,使其看起来像这样:

card=[2,3,4,5,6,7,8,9,10,11,12,13,14]

到处都有类似的东西

print card

替换为

print cardDict[card]

相关问题 更多 >

    热门问题