0

A little game for my very beginning python3 learning. The code as follow:

import random

def get_luck():
    lucky_num = random.randrange(0, 101)
    return lucky_num

def filter(num):
    if str.isdigit(num):
        if int(num)>=0 and int(num)<= 101:
            print('number' + num + 'is right, wish you luck')
            return num
        else:
            print('number not in range, type again')
            input_check()
    else:
        print('not a number, type again')
        input_check()
    return num

def input_num():
    input_num = input('type a number between 0 to 100 ===> ')
    print("you just type in : " + input_num)
    return input_num

def input_check():
    r = 0  # <==  just for tracking bug
    first_num = input_num()
    print('first_num is: ' + first_num)
    correct_num = filter(first_num)
    r = r + 1 #<== just for tracking bug
    print(r)
    print('correct_num is:' + correct_num)
    return correct_num

def run_game():
    lucky_num = get_luck()
    any_num = input_check()
    print(any_num, lucky_num)

run_game()

after run this code, i try first number 234 then try string "eee", and finally input the right number 66. I expect to see the out print 66 and the lucky number. But as the following result:

type a mumber between 0 to 100 ===> 234
you just type in is : 234
first_num is: 234
number not in range check again
type a mumber between 0 to 100 ===> eee
you just type in is : eee
first_num is: eee
not a number check again
type a mumber between 0 to 100 ===> 66
you just type in is : 66
first_num is: 66
got the right num = 66
1
correct_num is:66
1
correct_num is:eee
1
correct_num is:234
234 98

It seems that this code: print(r) print('correct_num is:' + correct_num)

loop three times in "youknow - what" reason ::)) !

Thanks for any body can help!

===========================================

Thanks for this Answer: Asking the user for input until they give a valid response I solved my bug :

import random

def get_luck():
    lucky_num = random.randrange(0, 101)
    return lucky_num


def get_input():
    while True:
        try:
            input_num = input('Number between 0 to 100 ===> ')
            if str.isalpha(input_num) or int(input_num)<0 or int(input_num)>100:
                raise ValueError
        except ValueError:
            print("Not a Number Or Number Not in Range")
            continue
        else:
            break
    return input_num

def run_game():
    lucky_num = get_luck()
    input_num = get_input()
    print(input_num, lucky_num)

run_game()

Hope can help others.

Community
  • 1
  • 1
  • Please try to think of a better question title. What is the actual problem that you encountered, how can future visitors know they have the same issue? – Martijn Pieters Jul 03 '16 at 09:49
  • You have two problems: you are using recursion instead of a loop (see the duplicate) and you are ignoring the return value of recursive calls. Because you ignore the `input_check()` return value in `filter()`, you never replace `num` and you get your older, wrong input instead. – Martijn Pieters Jul 03 '16 at 09:52
  • Thank you Martijn Pieters lead me to the right place. I've change the question title and hope it would be help. – Zerg.to_terrain Jul 03 '16 at 10:19

0 Answers0