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()