0

Disclaimer: noob here. I am currently studying Python in college, and code not only at school, but for fun. I am currently writing a planet generating code (which I fully wrote in smallbasic language) and working it into Python language. I am running into a slight problem, due to the inexperience I have with Python, but this may be an easy one for you guys. Please tell me how exactly I can improve this segment of code so it works in the simplest way possible. Also, I want to point out that I want the user to enter a number between 1 and 12 planets to generate. Also, if the user just presses ENTER, enters a number outside the 1-12 number range, or enters anything other than a number, I'm wanting the program to just pick a random number. The code below works how I want it too, but it just looks sloppy and overkill...

def num_planets():
    try:
        valid = ("1","2","3","4","5","6","7","8","9","10","11","12")
        count = int(input("How many planets would you like to generate? "))
        if count != valid:
            import random
            count = random.randint(1,12)
            print("Given the invalid user input, the random valid integer,",count,"was selected.")
    except ValueError:
        import random
        count = random.randint(1,12)
        print("Given the invalid user input, the random valid integer,",count,"was selected.")

num_planets()
Mr. T
  • 11,960
  • 10
  • 32
  • 54
Caleb
  • 3
  • 1
  • correction* the code above does not work. It always says input is invalid even when I chose a number between 1 and 12 – Caleb Apr 06 '18 at 04:36

1 Answers1

0

First of all, I don't think it's working the way you expect it to because count != valid is comparing an int to a tuple of strings. So that condition will always be True (so your code will always generate a random value).

You need to convert valid into a tuple of ints, then use the in operation to check if the int value is part of the tuple.

Like this:

valid = (1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12)
count = int(input("How many planets would you like to generate? "))
if count not in valid:
    # use a random value

Second, to avoid repetition, you could place the randomizer code in a method.

Like this:

def use_random_value():
    import random
    count = random.randint(1, 12)
    print("Given the invalid user input, the random valid integer,", count, "was selected.")


def num_planets():
    try:
        valid = (1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12)
        count = int(input("How many planets would you like to generate? "))
        if count not in valid:
            use_random_value()
    except ValueError:
        use_random_value()


num_planets()

Lastly, the try/catch for the int(..) is already "simple" enough as it follows the EAFP [1][2] pattern. It will clearly try to convert the input value to an int, then you can just easily handle the error if that fails.

Gino Mempin
  • 25,369
  • 29
  • 96
  • 135
  • This worked perfectly! Thank you Gino. I did not know you could include the NOT boolean logic into an IN operation. Now that I think of it, it makes sense:/ – Caleb Apr 06 '18 at 04:57
  • No problem :) By the way, I forgot to mention [EAFP](https://docs.python.org/3/glossary.html#term-eafp) as a coding style, which is implemented by that `try/catch` code. – Gino Mempin Apr 06 '18 at 05:32