mercredi 26 août 2020

Allowing program to covert repeating string to number in a guess game program

Question is, when the answer is a repeating number, eg 9 + 3 = 11 when transformed to text lets say a = 9, b = 3 , c = 1 hence it is a + b = cc. In my if statement, how to let the program acknowledge and convert both c to 1 when one correct guess for c was made?

you can see in the photo of the output where 1 correct guess was made for e but only one e was converted to the number. enter image description here

Another question if my def display(), what is the difference if I were to change all the other if statements after my first if statement to elif and deleting the indent to align to my first if statement. I initially did that but I couldn't get the desired output where the program revels the answer eventho I keyed in the wrong answer. I am really learning here, so it was all trial and error tbh i don't really understand what I was doing with the elif statements.

def getLetterMap():
    stringList = list("abcdefghij")
    shuffle(stringList)
    return "".join(stringList)


def display(letter, num1, num2, rsltTens, rsltOnes, drsltTens, drsltOnes, dnum1, dnum2):
    if dnum1 == -1 and dnum2 == -1 and drsltTens == -1 and drsltOnes == -1:
        print(" ", num1)
        print("+", num2)
        print("----")
        print(rsltTens, rsltOnes)
        print("----")

    if dnum1 == -1:
        print(" ", letter[num1])
    else:
        print(" ", num1)
    if dnum2 == -1:
        print("+", letter[num2])
    else:
        print("+", num2)
    print("----")

    if drsltTens == -1 and drsltOnes == -1:
        print(letter[rsltTens], letter[rsltOnes])

    elif drsltTens == rsltTens and drsltOnes == -1:
        print(rsltTens, letter[rsltOnes])

    elif drsltTens == -1 and drsltOnes == drsltOnes:
        print(letter[rsltTens], rsltOnes)
    else:
        print(rsltTens, rsltOnes)
    print("----")


def main():
    letter = getLetterMap()
    num1 = letter.find("h")
    num2 = letter.find("j")
    total = num1 + num2
    rsltTens = total // 10
    rsltOnes = total % 10
    drsltTens = -1
    drsltOnes = -1
    dnum1 = -1
    dnum2 = -1
    tries = 0
    while True:
        tries += 1
        display(letter, num1, num2, rsltTens, rsltOnes, drsltTens, drsltOnes, dnum1, dnum2)
        ltr = str(input("Enter a letter: "))
        digit = int(input("Enter a digits: "))
        if letter[digit] == ltr:
            print("You guessed {} for {} correctly. Well done!".format(digit, ltr))
            if ltr == letter[num1]:
                    dnum1 = num1
            elif ltr == letter[num2]:
                    dnum2 = num2
            elif ltr == letter[rsltTens]:
                    drsltTens = rsltTens
            elif ltr == letter[rsltOnes]:
                    drsltOnes = rsltOnes
            elif False:
                print("You got it in {} tries".format(tries))
        else:
            print("({},{}) is not correct. Try again.".format(ltr, digit))


main()

Aucun commentaire:

Enregistrer un commentaire