#πŸ”’ How can I fix the error I'm getting upon choosing odd as choice?

14 messages Β· Page 1 of 1 (latest)

indigo crystal
#
#currently, choice only supports if user starts from an odd number.
range1 = int(input(f"Please enter the first number of your range: "))
range2 = int(input(f"Please enter the second number of your range: "))
choice = input("Would you like all odd numbers, even numbers or none?")

if choice.lower() == "odd":
    if (range1 % 2) == 1:
        for x in range(range1, range2, 2):
            print(x)
    elif (range % 2) == 0:
        for x in range(range1+1,range2, 2):
            print(x)
elif choice.lower() == "even":
    if (range % 2) == 0:
        for x in range(range1,range2, 2):
            print(x)
    elif (range % 2) == 1: ## if modulus 2 is 1, the number is odd.
        for x in range(range1+1, range2, 2):
            print(x)
elif choice.lower() == "none":
    for x in range(range1, range2):
        print(x)
else:
    print("Appologies, pls try again.")```
soft flaxBOT
#

@indigo crystal

Python help channel opened

Remember to:

  • Ask your Python question, not if you can ask or if there's an expert who can help.
  • Show a code sample as text (rather than a screenshot) and the error message, if you've got one.
  • Explain what you expect to happen and what actually happens.

:warning: Do not pip install anything that isn't related to your question, especially if asked to over DMs.

#

Hey @indigo crystal!

Please edit your message to use a code block

Add a py after the three backticks.

```py
print('Hello, world!')
```

This will result in the following:

print('Hello, world!')```
idle copper
#

Possibly you should also show the error.

But I'd not that range is a builtin function, and that you might mean range2 in the lines with elif (range % 2)

indigo crystal
idle copper
#

Yeah. You're trying to do % on range, and range is actually a Python type. I think you mean range1 or range2 in these lines.

indigo crystal
#

true

#

that worked tysm

indigo crystal
#

Like can I use fewer lines for the same output(s), or is this optimal?

idle copper
#

I think I'd do 2 things:

  • use the even/odd choice to bump the starting point (range1) to be even or odd as chosen, if it isn't already
  • use the even/odd vs none choice to pick a step size of 2 or 1 respectively
  • then just have a single for x in range(range1, range2, step): loop
#

(and I'd say all instead of none; personally I'd take none to mean "don't print anything" πŸ™‚ )

soft flaxBOT
#
Python help channel closed for inactivity

This help channel has been closed. Feel free to create a new post in #1035199133436354600. To maximize your chances of getting a response, check out this guide on asking good questions.