繁体   English   中英

骰子和赌博游戏 Python

[英]Dice and gamble game Python

您好,我的代码有问题。 我想在方法 main 中检查 ran_dice 的值,但我不知道该怎么做。 例如,我写了 ran_dice(2) 它返回 2 个随机整数,我想检查这两个整数是否相等。 我可以在 main 方法中做吗? 如何 ?

打印ran_dice(2)应该可以解决问题。

根据评论编辑:

a,b=ran_dice(2)
if a==b:
    # code to stop

然而,正如另一条评论所提到的, ran_dice(s)函数有点危险,因为它返回的东西数量各不相同。 让程序返回一致数量的东西是一种很好的做法。 您可以返回列表中的值,并且列表的大小可能会有所不同,但至少您总是返回一个列表。

这是一个示例,您可以在列表中返回骰子值。 所以你可以随意调整返回的骰子数量,并比较结果。

import random

def ran_dice(s):
    if s==1:
        a=random.randint(1,6)
        return [a]
    elif s==2:
        a=random.randint(1,6) 
        b=random.randint(1,6) 
        return [a,b]
   

def main():
    credit=100
    print('Welcome user, you have ', credit,'credits.')
    number=int(input('How much do you want to gamble?: '))
    while number <0 or number>100:
        print('You need to give a positive integer no more than your credit.')
        number=int(input('How much do you want to gamble?: '))
    result = ran_dice(2)
    print ("dice=", result)
    firstdice = 0
    for dice in result:
        if firstdice == 0:
            firstdice = dice
        elif dice == firstdice:
            print("equal")
        else:
            print("different")

    if result[0] == result[1]:
        print("equal")

main()

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM