简体   繁体   中英

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. For example I wrote ran_dice(2) it return 2 random integers and I want to check these two integers equals or not. Can I do in main method ? How ?

Printing ran_dice(2) should do the trick.

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. 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()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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