求助!用python编写21点程序的问题。。。

哪位好心的python大牛帮写一下21点的程序??万分感谢!!!

import random

# 1
def ask_user(prompt, response1, response2):
    """
    Ask user for a response in two responses.
    prompt (str) - The prompt to print to the user
    response1 (str) - One possible response that the user can give
    response2 (str) - Another possible response that the user can give
    """
    
    while True:
        # ask user for response
        user_response = input(prompt)

        #if response is response1 or response2 then return
        if user_response == response1 or user_response == response2:
            return user_response


# 2
def print_card(name, num):
    """
    print "______ draws a _____" with the first blank replaced by the user's
    name and the second blank replaced by the value given to the function.
    name (str) - the user name to print out
    num (int) - the number to print out
    """

    # if the value is a 1, 11, 12, 13, print Ace, Jack, Queen, King 
    if num == 1:
        num = "Ace"
    elif num == 11:
        num = "Jack"
    elif num == 12:
        num = "Queen"
    elif num == 13:
        num = "King"
    else:
        num = str(num)

    # print the string
    print(name, "draws a", num)
        


# 3
def get_ace_value():
    """
    Ask the user if they want to use a 1 or 11 as their Ace value.
    """

    # get the value use "ask_user" function
    value = ask_user("Should the Ace be 1 or 11?", "1", "11")

    # retrun the value
    return int(value)



# 4
def deal_card(name):
    """
    Pick a random number between 1 and 13, and print out what the user drew.
    name (str) - the user name to print out
    """

    # get a random number in range 1 to 13
    num = random.randrange(1, 13+1)

    # use "print_card" function to print out what the user drew
    print_card(name, num)


    if num > 10:
        # if the card is a Jack, Queen, or King, the point-value is 10
        return 10
    elif num == 1:
        # If the card is an Ace, ask the user if the value should be 1 or 11
        return get_ace_value()
    else:
        return num
        

    
# 5
def adjust_for_bust(num):
    """
    If the given number is greater than 21, print "Bust!" and return -1.
    Otherwise return the number that was passed in.
    num (int) - the given number
    """

    # determine the value of num
    if num > 21:
        print("Bust!")
        return -1
    else:
        return num



    
# 6
def hit_or_stay(num):
    """
    Prompt the user hit or stay and return user's chose.
    num (int) - the value of a player's card hand
    """

    if num <= 21:
        chose = ask_user("Hit or stay?", "hit", "stay")
        # if num less than 21 and user chose hit return True
        if chose == "hit":
            return True
        
    # otherwise return False
    return False
            



    
# 7
def play_turn(name):
    """
    Play whole the trun for a user.
    name (str) - the player's name
    """

    # print out that it's the current players turn
    print("==========[ Current player:", name, "]==========")

    # set total score zero
    total_score = 0

    # deal the player a card for loop
    while True:

        # get total score
        total_score += deal_card(name)

        # if not busted print out the player's total score
        print("Total:", total_score)


        # if player chose stay return the result, otherwise continue the loop
        if not hit_or_stay(total_score):
            return adjust_for_bust(total_score)



    
# 8
def determine_winner(name1, name2, score1, score2):
    """
    Determine_the game's winner.
    name1 (str) - the first player's name
    name2 (str) - the second player's name
    score1 (str) - the first player's score
    score2 (str) - the second player's score
    """

    if score1 == score2:
        print(name1, "and", name2, "tie!")
    elif score1 > score2:
        print(name1, "wins!")
    elif score1 < score2:
        print(name2, "wins!")


# 9
def main():
    """
    The main program of BlackJack game
    """

    while True:

        # Ask each player for their name
        name1 = input("Player 1 name:")
        name2 = input("Player 2 name:")

        # Greet them
        print("Welcome to BlackJack", name1, "and", name2)
        print()

        # Let the first player play a turn
        score1 = play_turn(name1)
        print()

        # Let the second player play a turn
        score2 = play_turn(name2)
        print()

        # Determine who won
        determine_winner(name1, name2, score1, score2)

        # Play again if they say yes and end the loop if they say no
        if ask_user("Would you like to play again?", "yes", "no") == "no":
            break


if __name__ == "__main__":
    main()


如果你的作业正好也是这个的话,千万别一模一样抄哈,按这参考自己改一改,不然会被判雷同的。

温馨提示:答案为网友推荐,仅供参考
第1个回答  2013-11-18
自己动手吧。可以提供一下思路:
定义一个1--13的随机数,每次给每次询问是否要牌,要就随机再发一张,不要就询问下一个人,对每个人的数字总和进行计算和对比,按照游戏规则评出哪个获胜。
这个个人觉得就是一个字典、数组、循环和条件判断的处理。当然如果你要图形界面,那还得额外处理。
思路仅供参考,代码还希望自己能写。追问

就是不知道具体怎么码代码...什么编程语句的完全没概念...不知如何下笔...

第2个回答  2019-12-14
vivo手机忘记锁屏密码可通过以下方法找回密码: 1、在锁屏界面连续输错五次锁屏密码,点击忘记密码(没有此选项可参考下面方法)通过锁屏密保修改锁屏密码。 2、可以携带手机前往当地客户服务中心解锁,会有专业人员帮您妥善处理的。进入vivo官网app--服务--我的服务--服务网点即可查询到就近的客户服务网点地址及电话。
第3个回答  2013-11-18
...你这提问也太简单粗暴了吧追问

思路什么的作业都给出来了,就是完全不知道怎么开头,中间,结尾...〒_〒

相关了解……

你可能感兴趣的内容

本站内容来自于网友发表,不代表本站立场,仅表示其个人看法,不对其真实性、正确性、有效性作任何的担保
相关事宜请发邮件给我们
© 非常风气网