-7

I just tried to write the code below that takes only a whole number between 1 to 10 and prints some statements of the input value does not fulfill the required conditions.

gamelist = list(range(0,11))

def display_game(gamelist):
    print('Here is your list of options:')
    print(gamelist)

def user_choice():
    choice = 'x'                      
    acceptable_range = range(0,11)
    within_range = False

    while choice.isdigit() == False or within_range == False:
        choice = input('Please enter a digit from the above options:')
    
        if not choice.isdigit():
            print("Oops! That's not a digit. Please try again.")
    
        if choice.isdigit():
            if int(choice) in acceptable_range:
                within_range == True
            else:
                within_range == False
                print('The entered number is out of acceptable range!')
           
    return int(choice)

display_game(gamelist)
user_choice()

And after running this code, even after entering the correct value it keeps on asking for the input.I am not understanding what's gone wrong exactly and where.

Riccardo Bucco
  • 13,980
  • 4
  • 22
  • 50
  • Please do some online research; SO is not the best place for this question. Recommend closure. – S3DEV Aug 05 '20 at 06:07
  • See https://stackoverflow.com/questions/2677185/how-can-i-read-a-functions-signature-including-default-argument-values – Selcuk Aug 05 '20 at 06:07
  • Look at the function's documentation. – Minh-Long Luu Aug 05 '20 at 06:09
  • Kindly check how to ask, do a simple Google search (for such a question) and you will find a lot of resources. Moreover, @AnushkaBhakare search the source code and documentation. It would suffice, in the beginning you can also KIte for additional help in text editor. – Astrian_72954 Aug 05 '20 at 06:10
  • It is not clear from the question whether you are asking how to understand the source code of the function to see what arguments it takes - in which case, read any basic Python reference - or how to obtain the function signature from a function object programmatically, in which case see the answer which @Selcuk linked. – alani Aug 05 '20 at 06:14

2 Answers2

0

Arguments are generally passed to give the function access to the data is has no access to but is required to work upon the task it is assigned to do. A good example would be if you call a function from inside other function and want to use data of local variable of your former function into latter, you would have to pass it as argument/arguments . This is a very vague example just to give you some basic hint , basically there are many factors you would consider before deciding whether or not you need arguments in your function!!

Dhruva-404
  • 168
  • 2
  • 10
0

== is for comparison, = is for assigment:

gamelist = list(range(0, 11))


def display_game(gamelist):
    print("Here is your list of options:")
    print(gamelist)


def user_choice():
    choice = "x"
    acceptable_range = range(0, 11)
    within_range = False

    while choice.isdigit() == False or within_range == False:
        choice = input("Please enter a digit from the above options:")

        if not choice.isdigit():
            print("Oops! That's not a digit. Please try again.")

        if choice.isdigit():
            if int(choice) in acceptable_range:
                # within_range == True  # this doesn't assign, but compares
                within_range = True  # this assigns
            else:
                # within_range == False  # And the same here
                within_range = False
                print("The entered number is out of acceptable range!")

    return int(choice)


display_game(gamelist)
user_choice()

This prevents the endless loop. Comparing to booleans is not really necessary. Keeping the same idea this version is a bit more readable:

def user_choice():
    choice = "x"
    acceptable_range = range(0, 11)

    while not choice.isdigit() or not within_range:
        choice = input("Please enter a digit from the above options:")
        
        if not choice.isdigit():
            print("Oops! That's not a digit. Please try again.")
        else:
            within_range = int(choice) in acceptable_range
            if not within_range:
                print("The entered number is out of acceptable range!")

    return int(choice)

Or even a version where choice is never a string. But maybe you haven't gotten to Exceptions yet :-). (this will change the message for '-1', though.)

def user_choice():
    choice = None
    acceptable_range = range(0, 11)

    while choice not in acceptable_range:
        try:
            choice = int(input("Please enter a digit from the above options:"))
        except ValueError:
            print("Oops! That's not a digit. Please try again.")
        if choice not in acceptable_range:
            print("The entered number is out of acceptable range!")
    return choice
Chris Wesseling
  • 6,226
  • 2
  • 36
  • 72