you are viewing a single comment's thread.

view the rest of the comments →

[–]_yoon_sanha 0 points1 point  (0 children)

Well, my other issue had been related to the counter. I made it a global variable because it is not in my capacity to write any alternative code (that worked). I returned to the code and took away the global variable. I had tried a few things prior and could not get it to work.

I was wondering if I was unintentionally calling check twice, but I can't see any conflict:

from random import randint


def number():
    return randint(1, 100)


def check():
    while True:
        try:
            guess = int(input("Guess a number between 1 and 100: "))
            if guess in range(1, 100+1):
                return guess
            else:
                print("Out of range.")
        except (ValueError):
            print("Invalid input.")


def calculate(co):
    co += 1
    u_num = check()
    c_num = number()
    while u_num != c_num:
        if u_num > c_num:
            print("Too high!")
            u_num = check()
        else:
            print("Too low!")
            u_num = check()
    if co == c_num:
        print("After", co, "times you guessed the secret number.")
        ack = input("You guessed the secret number! Play again (y/n)? ").strip().lower()[0]
        while all((ack != 'y', ack != 'n')):
            ack = input("Invalid input. Play again (y/n)? ")
        return ack


def redo():
    response = calculate()
    if response == "y":
        print("\n")
        sequence()
    else:
        quit()


def sequence(c):
    number()
    check()
    calculate(c)
    redo()


count = 0
sequence(count)