Welcome to Code Forum!

Join a community that supports you and your coding journey from day one. We strive to be a friendly, supportive community that empowers everyone to be better developers. By registering with us, you'll be able to discuss, share and private message with other members of our community.

SignUp Now!
  • Guest, before posting your code please take these rules into consideration:
    • It is required to use our BBCode feature to display your code. While within the editor click < / > or >_ and place your code within the BB Code prompt. This helps others with finding a solution by making it easier to read and easier to copy.
    • You can also use markdown to share your code. When using markdown your code will be automatically converted to BBCode. For help with markdown check out the markdown guide.
    • Don't share a wall of code. All we want is the problem area, the code related to your issue.

    GIF shows where to locate </> in the thread and or post editor toolbar.
    To learn more about how to use our BBCode feature, review our "How to post your code into threads" here.

    Thank you, Code Forum.

Python Issue

Steazr

New Coder
I'm trying to make a function that lets you choose a number between 1 and 2, and use the last number if anything else is used. I do this, but everytime the if statement is failing me, and instead just runs the code inside of the if statement even if the conditions aren't met. Here is the code
Python:
import Vars


def choose():
    Prompt1 = input("Type a num
    else:
        print("Invalid number, using last number...")
    Prompt2 = input("Type a number 1-2. This is your SECOND number choice.\n")
    Choice2 = int(Prompt2)
    if Choice2 > 2 or Choice2 <= 0:
        Vars.No2 = Choice2
    else:
        print("invalid number, using last number...")
    print(Vars.No1, Vars.No2)
if you can help me solve this, I would be incredibly thankful.
 
Not sure that you understand how if else works.
I don't know of any module called Vars so, I can't comment on that
To point out a couple of mistakes in your code:
1. You're first prompt has no closing bracket
2. You have an else statement with no if

Here is an example of how it could be.

Python:
# Define a function
def choose():
    default = 1 # settinga default value. Needed if 1 or 2 is nit entered
    number = int(input('Type either 1 or 2.\n>> ')) # Settingup the prompt. Making it type int

    '''
    Checking type of enetered value is a number is greater than 0 and less than or = 2
    assingning number to another variable
    else print the error and assign number a default value
    '''
    if isinstance(number, int) and number > 0 and number <= 2:
        another_var = number
    else:
        print('Invalid number. Using default')
        another_var = default

    print(f'Default: {default}, number: {number}, another_var: {another_var}')

# Call the function
choose()
 
Last edited:
Not sure that you understand how if else works.
I don't know of any module called Vars so, I can't comment on that
To point out a couple of mistakes in your code:
1. You're first prompt has no closing bracket
2. You have an else statement with no if

Here is an example of how it could be.

Python:
# Define a function
def choose():
    default = 1 # settinga default value. Needed if 1 or 2 is nit entered
    number = int(input('Type either 1 or 2.\n>> ')) # Settingup the prompt. Making it type int

    '''
    Checking type of enetered value is a number is greater than 0 and less than or = 2
    assingning number to another variable
    else print the error and assign number a default value
    '''
    if isinstance(number, int) and number > 0 and number <= 2:
        another_var = number
    else:
        print('Invalid number. Using default')
        another_var = default

    print(f'Default: {default}, number: {number}, another_var: {another_var}')

# Call the function
choose()
Thanks, for some reason I accidentally deleted like a chunk of my code and didn't realize it. Thanks!
 

Buy us a coffee!

Buy me a coffee.
Back
Top Bottom