lundi 28 août 2017

Int(input) linked to a conditional if statement doesn't output correctly, and else statement callback forms an infinite recursion loop

(I'm still new to forum posting, apologies if this is laid out/worded poorly)

Please could you help me solve this. My question is that I'm trying to get the user to input an integer that is valid to a dice, eg, 6, 12, 20 etc. (I only have it set up for D6 and D20's currently) for my dice rolling application that I aim to work like a standalone version to what is used on Roll20.net,

I have two issues:

1.) When the user picks a valid die, eg 6 or 20, rather than rolling said die, it just returns the number 6 or 20, or any number you type in past the first else failure, eg 12, 76, 9709487. When instead it should post either a 1 or a 2 with the respective print statement as I have randint set to (1,2) testing the print statements.

2.) Trying to callback the pick_dice function for another attempt causes the program to fall into a recursion loop, and trying to use a break statement also causes me to have an error. If a string is attempted as an input a ValueError is thrown up rather than just the else statement. I'm finding this extremely confusing, I'm not even sure if I am using the correct conditionals (I find the python documentation extremely confusing and I cannot find anything on there that makes sense to help me solve my issue).

My Code:

import random
from random import randint

d20 = randint(1,2)  #for test purposes rather than (1,20)
d6 = randint(1,2)

str1 = "You rolled a "
str2 = "Uh Oh, Critical Fail"
str3 = "Congrats Critical Hit"

#num = d20
#To select a die - More will be added later e.g. d10, d12, d100
num = int(input("Please pick a valid die: 6 or 20: "))


def roll(d20):
    print(str1, d20)

    if d20 == 1:
        print(str2)    
    elif d20 == 2:
        print(str3)

#roll(d20)

def roll(d6):
    print(str1, d6)

    if d6 == 1:
        print(str2)    
    elif d6 == 2:
        print(str3)

#roll (d6)


def dice():
    if num == d20:
        roll(d20)
    elif num == d6:
        roll(d6)

dice()


def pick_dice():
    print (num)
    if num is 6 or num is 20:
        dice()
    #elif num != 6 or num != 20:
    else:
        print ("Please input a valid die")
        #I would put a break statement here but it causes an error
pick_dice()

Should I just give up on the user input and try and build a GUI to where I just use a combobox within Tkinter for Dice selection, and buttons to call functions? As I aim to build a GUI for this program at some point. I'm really struggling to understand these basics.

I hope somebody out there is able to help me or point me in the right direction, thank you.




Aucun commentaire:

Enregistrer un commentaire