简体   繁体   English

骰子和赌博游戏 Python

[英]Dice and gamble game Python

Hello I have a problem in my code.您好,我的代码有问题。 I want to check valu of ran_dice in method main but I dont know how I can do.我想在方法 main 中检查 ran_dice 的值,但我不知道该怎么做。 For example I wrote ran_dice(2) it return 2 random integers and I want to check these two integers equals or not.例如,我写了 ran_dice(2) 它返回 2 个随机整数,我想检查这两个整数是否相等。 Can I do in main method ?我可以在 main 方法中做吗? How ?如何 ?

Printing ran_dice(2) should do the trick.打印ran_dice(2)应该可以解决问题。

Edit according to the comment:根据评论编辑:

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

As another comment mentioned, however, the ran_dice(s) function is a little dangerous as the amount of things it returns varies.然而,正如另一条评论所提到的, ran_dice(s)函数有点危险,因为它返回的东西数量各不相同。 It's good practice to have a program return a consistent amount of things.让程序返回一致数量的东西是一种很好的做法。 You could return the values in a list, and the size of the list could vary, but at least you're always returning one list.您可以返回列表中的值,并且列表的大小可能会有所不同,但至少您总是返回一个列表。

Here is an example, you could return the dice values in a list.这是一个示例,您可以在列表中返回骰子值。 So you can adjust the returned number of dice as you like, and compare the result.所以你可以随意调整返回的骰子数量,并比较结果。

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