不知道如何覆盖随机

时间:2015-01-25 05:10:34

标签: python python-2.7 random

import random
import time
import math
import sys

card_numbers = [1,2,3,4,5,6,7,8,9]
card_suite = ["of hearts", "of diamonds", "of clubs", "of spades", "Queen", "King", "Jack", "Ace"] #4,5,6,7
random_number = random.choice(card_numbers)
random_number2 = random.choice(card_numbers)
random_number3 = random.choice(card_numbers)
random_suite = random.choice(card_suite)

numberadd = random_number + random_number2
numberadd2 = random_number + random_number2 + random_number3

def setup():
    setupinput = raw_input("Type deal to deal the cards!")
    if setupinput == "deal":
        deal()
    elif setupinput != "deal":
        goodbye()
    else:
        print "Invalid Syntax!" 
        sys.exit(0)

def deal():
    print "Your first card is... ", random_number, random_suite
    print "Your second card is... ", random_number2, random_suite
    if numberadd >= 21:
        retry()
    else:
        thirdround()

def thirdround():
    thirdroundinput = raw_input("Would you like another card?")
    if thirdroundinput == "yes":
        print "Your next card is... ", random_number3, random_suite
        if numberadd2 >= 21:
            retry()
        else:
            print "You win! Your total was... ", numberadd2
            retry()
    elif thirdroundinput == "no":
        print "Okay... safe! Your total was... ", numberadd
    else:
        print "Invalid Syntax!"
        sys.exit(0)

def goodbye():
    print "Okay... goodbye!"
    sys.exit(0)

def retry():
    retryinput = raw_input("Would you like to try again?")
    if retryinput == "yes":
        setup()
    elif retryinput != "yes":
        goodbye()
    else:
        print "Invalid Syntax!"
        sys.exit(0)

def ifblacklist():
    if random_number or random_number2 or random_number3 == "Queen" or "King" or "Jack":
setup()

全新的编码,刚完成编码(几乎是作为初学者项目完成)并且想知道,如果随机套件选择了王牌,王牌,王牌或者杰克,或者我做它以便输出数字10(或1为王牌)?

由于

为了澄清,ifblacklist功能是我尝试输出数字10,并且想知道是否有人能指出我正确的方向完成它/重写

1 个答案:

答案 0 :(得分:1)

首先,所有else块都是这样的;

retryinput = raw_input("Would you like to try again?")
    if retryinput == "yes":
        setup()
    elif retryinput != "yes":
        goodbye()
    else:
        print "Invalid Syntax!"
        sys.exit(0)

没必要。因为您的程序永远不会处理else,因为elif retryinput != "yes":包含所有情况。所以你应该删除那些没用的其他块或者 你应该改变它们;

retryinput = raw_input("Would you like to try again?")
    if retryinput == "yes":
        setup()
    elif retryinput == "no":
        goodbye()
    else:
        print "Invalid Syntax!"
        sys.exit(0)

其次,这个功能

def ifblacklist():
   if random_number or random_number2 or random_number3 == "Queen" or "King" or "Jack":
setup()

应该是;

if random_number=="Queen" or random_number2=="King" or random_number3=="Jack":
    do something

否则它将永远是True,你应该写出所有条件/同等。您如何确保这些数字为10;

你可以这样做;制作一个字典并输入这样的键/值;

mydict={"Queen":10,"King":10...}

然后在这个字典中搜索你的卡片,并将它们的值相加。