#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2013
    Location
    Newcastle-Upon-Tyne
    Posts
    1
    Rep Power
    0

    Python noob needs a hand.


    Hey guys,

    I've just started learning how to code in Python 3.0, and I've run across a couple of bugs in my first program.

    I've split the program into four functions, and have two problems at the moment. Firstly the list guesses_total isn't picking up the .appends in the main body of code and is returning the length of the list as 0 when in fact it should be 7 or 8 items long. The second major problem is that when you enter a string for var 'menu' it returns the correct values but also returns the string describing how far 'guess' is from the correct answer.

    Anyway, I hope this makes sense, any help would be greatly appreciated, as would an explanation of why my code is flawed. I'm fairly certain all these bugs come from my half understanding of the principals I'm working with.

    Cheers guys!

    Code:
    import random
    import sys
    import os
    num = 0
    guess = -1
    guess_high = []
    guess_low = []
    guess_total = guess_high+guess_low
    restart = ' '
    score = len(guess_total)
    
    def program_game():
        global num
        num = random.randint(1,51)
        global guess_high
        global guess_low
        global guess_total
        global guess
        clue1 = random.randint(4,7)
        clue2 = random.randint(6,9)
        menu = " "
        print('Guess the number, between 1 and 50!')
        print("Type 'help' for menu.")
        while guess != num:
            try:
                guess = int(input('Guess: '))
            except ValueError:
                    print("Type 'guesses' for list of attempts so far.")
                    print("Type 'clue' for a hint.")
                    print("Type 'exit' to quit.")
                    menu = input('Menu: ')
                    if menu == 'guesses':
                        print('Low - ',guess_low)
                        print('High - ',guess_high)
                    elif menu == 'clue':
                        print("It's between ",(num-clue2),' and ',(num+clue1))
                    elif menu == 'cheat':
                        print(num)
                    else:
                        sys.exit()
                        
            if guess == (num+1):
                guess_high.append(guess)
                print('SO close!')
            elif guess == (num-1):
                guess_low.append(guess)
                print('SO close!')
            elif guess in range(num,(num+5)):
                guess_high.append(guess)
                print('Too high!')
            elif guess in range((num-5),num):
                guess_low.append(guess)
                print('Too low!')
            elif guess in range(num,(num+10)):
                guess_high.append(guess)
                print('WAY off! Too high!')
            elif guess in range((num-10),num):
                guess_low.append(guess)
                print('WAY off! Too low!')
            elif guess > (num+11):
                guess_high.append(guess)
                print('Sky high...')
            elif guess < (num-11):
                guess_low.append(guess)
                print('Why even bother? Too low...')
            else:
                print(' ')
        global score
        score = len(guess_total)
        
    
    def program_intro():
        print('Hi!')
        p = input('Want to play a game? [Y/N]: ')
        if p == 'Y':
            program_game()
        else:
            print('Sorry to hear it!')
    
    
    def program_end():
        global score
        score = len(guess_total)
        print('Correct! Got it in ',score)
        if score == 1:
            print("WHAT?! One guess?!")
        elif score == 2:
            print('Well done! Two is pretty good!')
        elif score == range(3,6):
            print('Not bad. Respectable')
        elif score <= 10:
            print('Could do better')
        elif score >= 10:
            print('Really? I expected better...')
        else:
            print('Well at least you spelled your name corre...oh. We never asked for your name...')
        program_restart()
    
    
    def program_restart():
        global restart
        restart = input('Do you want to play again? [Y/N]: ')
    
        
    program_intro()
    if guess == num:
        guess_high.append(guess)
        program_end()
        global restart
        if restart == 'Y':
            program_game()
        else:
            print('Goodbye! [Hit enter to quit]')
            input(' ')
            sys.exit()
  2. #2
  3. Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2012
    Location
    39N 104.28W
    Posts
    158
    Rep Power
    3
    Right off the bat you define "guess_total" as the sum of 2 blank lists. Merely changing those lists will not change that now distinct list.
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2009
    Posts
    475
    Rep Power
    33
    Take a look at the difference between extend and append for lists. You'll have to search for it. Also, the length of guess_total will always be 2 because it contains 2 lists. You will have to use the sum of the length of guess_total[0]+guess_total[1], or len(guess_high)+len(guess_low).
    Code:
    guess_high = []
    guess_low = []
    guess_total = [guess_high, guess_low]
    guess_low.append(5)
    print guess_total
    guess_high.append(2)
    print guess_total
    The second major problem is that when you enter a string for var 'menu' it returns the correct values but also returns the string describing how far 'guess' is from the correct answer.
    It prints what you tell it to print and you do tell it to print other things depending on what was entered. I would suggest code more like the following
    Code:
    menu=""
    while menu not in ["guesses", "clue", "cheat", "exit"]:
        print("Type 'guesses' for list of attempts so far.")
        print("Type 'clue' for a hint.")
        print("Type 'cheat' to print the number.")
        print("Type 'exit' to quit.")
        menu = input('Menu: ')
    
        if menu == 'guesses':
            print('Low - ',guess_low)
            print('High - ',guess_high)
        elif menu == 'clue':
            print("It's between ",(num-clue2),' and ',(num+clue1))
        elif menu == 'cheat':
            print(num)
        elif menu -- "exit":
            return
    Last edited by dwblas; August 29th, 2013 at 10:35 AM.

IMN logo majestic logo threadwatch logo seochat tools logo