dl_atl
dl_atl

Reputation: 107

Python input, unwanted recurring prompt

I am writing code in python for an online course. The code should accept input integers from 0-10, and prompt the user to enter other values if these are not written into the input prompt.

Here is my code:

def user_choice(): 
    
    #Initial
    choice = 'WRONG'
    acceptable_range = range(0,11)
    within_range = False 
    
    #Two conditions to check 
    #Digit and within_range 
    while choice.isdigit() == False or within_range == False: 
    
        choice = input("Please enter a number 0-10: ")
        
        #Digit check 
        if choice.isdigit() == False: 
            print('Sorry that is not a digit.')
        
        #Range check 
        if choice.isdigit() == True:
            if int(choice) in acceptable_range: 
                within_range == True 
                
            else:
                print('Sorry, out of acceptable range.')
                within_range == False
    
    return int(choice) 

After running this, I can see the input prompt. If I input a letter or number besides 1-10, I get the appropriate message. If I enter a number 1-10, the function runs and displays that number, but does not end the input with an Out [] output like it should. It asks for another input. Why do I still get the input prompt?

Upvotes: 0

Views: 201

Answers (3)

G Ritchie
G Ritchie

Reputation: 146

You are trying to assign within_range with == instead of =.

It should be

within_range = True 

Using == is for comparing values, = is to assign values to a variable.

Upvotes: 1

ComteHerappait
ComteHerappait

Reputation: 97

In python, var = 5 is a variable affectation and var == 5 is an equality check.

replace within_range == False by within_range = False

Upvotes: 1

Mohammad
Mohammad

Reputation: 3396

I believe you have a typo for the == should be =

def user_choice(): 
    
    #Initial
    choice = 'WRONG'
    acceptable_range = range(0,11)
    within_range = False 
    
    #Two conditions to check 
    #Digit and within_range 
    while choice.isdigit() == False or within_range == False: 
    
        choice = input("Please enter a number 0-10: ")
        
        #Digit check 
        if choice.isdigit() == False: 
            print('Sorry that is not a digit.')
        
        #Range check 
        if choice.isdigit() == True:
            if int(choice) in acceptable_range: 
                within_range = True 
                
            else:
                print('Sorry, out of acceptable range.')
                within_range = False
    
    return int(choice) 

Upvotes: 4

Related Questions