Python程序,其中有一个或两个玩家可供选择

时间:2013-12-04 02:17:29

标签: python python-3.x

Python 3问题......

该作业与向单人骰子掷骰游戏添加第二个玩家选项有关。如果有两个玩家,则两个玩家轮流滚动直到终止,然后计算每个玩家的滚动总数。一个玩家的想法相同。

以下是没有第二个玩家选项的单人游戏代码 -

    import random
    INTMIN=1
    INTMAX=6

    def welcome():
        print("Welcome to the Roll the Dice game!")
        player=input("What is your name? ")
        return player

    def roll():
        con='yes'
        while con=='yes':
            die_one=random.randint(1,6)
            die_two=random.randint(1,6)
            v=is_doubles(die_one,die_two)
            if not v:
                print("You rolled  " + str(die_one) + " and " + str(die_two))
            con=input("Do you want to continue yes/no: ")

    def is_doubles(die_one,die_two):
        if die_one==die_two:
            print("You have doubles: " + str(die_one) + " and " + str(die_two))
            return True
        else:
            return False

    def main():
        n=welcome()
        roll()
        print("Thank you for playing, " + n)
        input("End of program.")

    main()

谢谢。

1 个答案:

答案 0 :(得分:3)

这是一个快速而肮脏的解决方案(python3):

(lambda f,r,p1,p2:print('\n\n{} has a score of {}.\n{} has a score of {}.\n\nTh'
'ank you for playing.'.format(*(lambda a,b,c,d:(a,c,b,d))(p1,p2,*(lambda f,r,p1,
p2:f(f,r,p1,p2,0,0,*[r.randint(1,6) for _ in 'babe']))(f,r,p1,p2)))))(lambda f,r
,p1,p2,a1,a2,r1,r2,r3,r4:f(f,r,p1,p2,a1+r1+r2,a2+r3+r4,*[r.randint (1,6)for _ in
'babe'])if input(('\n{} has rolled{}: {} and {}.\n'*2+'\nDo you want to continu'
'e? [yes/*] ').format(p1,' a double' if r1==r2 else'',r1,r2,p2,' a double' if r3
==r4 else'',r3,r4))=='yes'else(a1+r1+r2,a2+r3+r4),__import__('random'),input('W'
'elcome\nWhat is your name? '),input('What is your name? '))

如果我是你的老师,我会对这个答案感到非常惊讶。

请记住以下几点:

  • 可读性很重要。
  • 变量名称适用于sissies。
  • 代码很难写,应该很难阅读。

<强>严重:

没有任何努力,很少有人会做你的功课。