1
\$\begingroup\$

I am trying to make an inputin() function to take all the parameters input for the first time with exception and for individual parameter for the runtime proposition. I am thus looking forward to reducing code size or being a more efficient coder with more pythonic procedures.

""This code is well executed and tested for all possible situations""

import turtle              # 1.  import the modules
#import random

BackGround = 'white'
Numside = 0
Armlen = 0
Turtlecolor = 'blue'
Turtleshape = 'turtle'

def inputin(count = 1, port = 0):
            #counting for resolving runtime error
    global BackGround, Numside, Armlen, Turtlecolor, Turtleshape

    while True :
        try :
            if((port == 0 or port == count) and count == 1):
                count+=1
                BackGround = str(input("Enter the Background Color : "))
            if((port == 0 or port == count) and count == 2):
                count+=1
                Numside = int(input("Enter the No. of Sides : "))
            if((port == 0 or port == count) and count == 3):
                count+=1
                Armlen = int(input("Enter the length of each side : "))
            if((port == 0 or port == count) and count == 4):
                count+=1
                Turtlecolor = str(input("Enter the Turtle Color : "))
            if((port == 0 or port == count) and count == 5):
                count+=1
                Turtleshape = str(input("Enter the Turtle Shape : "))
            port = -1
            break
        except ValueError :
            print("Oops!  That was no valid number.  Try again...")
            count+=-1

#Creating Screen after Everything Is Over


#i am creting this class for adjusting multiple values like a structure.
class Team(object):
    def __init__(self, Numside = 0, Armlen = 0, Turtlecolor = "", Turtleshape = ""):
        self.Numside = Numside
        self.Armlen = Armlen
        self.Turtlecolor = Turtlecolor
        self.Turtleshape = Turtleshape

def screen():
    wn = turtle.Screen()       # 2.  Create a screen

    while True:
        try:
            wn.bgcolor(BackGround)
            break
        except turtle.TurtleGraphicsError as e:
            inputin(1,1)

    wn.exitonclick()

def TurtleCreation():
    lance = turtle.Turtle()
    while True:
        positionInput = 0 #for marking the position of the desired input  
        try:
            positionInput = 4
            lance.color(Turtlecolor)
            positionInput = 5
            lance.shape(Turtleshape)
            break
        except turtle.TurtleGraphicsError as f:
            inputin(positionInput,positionInput)
    lance.speed(500)
    lance.penup()
    #lance.goto(-300,200)


inputin()
TurtleCreation()
screen()


#sc = turtle.ScrolledCanvas(master)

#now shifting the turtle to the middle of the system
\$\endgroup\$
2
  • 3
    \$\begingroup\$ count+=-1?! You came to the right place... \$\endgroup\$
    – jonrsharpe
    Commented May 26, 2015 at 21:44
  • \$\begingroup\$ So you really expect the function to be called with non-default parameters? Also, are BackGround, Numside, etc. local variables? \$\endgroup\$ Commented May 27, 2015 at 6:01

1 Answer 1

3
\$\begingroup\$

The whole port and count thing is a total waste of time. If you were stuck with it:

  • port is redundant, as break is what actually ends the loop;
  • If port was being used (e.g. while port:) it should be a bool (True/False) rather than an int (1/0);
  • You shouldn't have whitespace around the = for default argument values (def inputin(count=1, port=0):);
  • The if conditions should have more space and fewer parentheses: if port == 0 and count == 3:; and
  • count+=-1 should be count -= 1.

Only two of your inputs actually need to be numbers, yet you wrap the whole thing in try; that block should be as short as you can make it. You can factor out the integer inputs for much cleaner code (see e.g. Asking the user for input until they give a valid response). For the other three inputs the return from input is always a string, so the str calls are totally redundant.

Compare:

def int_input(prompt):
    """Validate integer-only user input."""
    while True:
        try:
            return int(input(prompt))
        except ValueError:
            print("Please enter an integer.")

def input_in():
    """Get required user inputs."""
    background = input("Enter the background color: ")
    num_sides = int_input("Enter the number of sides: ")
    arm_len = int_input("Enter the length of each side: ")
    turtle_color = input("Enter the turtle color: ")
    turtle_shape = input("Enter the turtle shape: ")
    ...

Note the adherence to the style guide and use of docstrings to explain what each function does.

As additional exercises:

  • Add limits to the integer validation, allowing a minimum and maximum to be set for the function (a negative arm_len wouldn't make much sense, for example);
  • Create a color_input function to validate the user's color selection, and use it where appropriate; and
  • Create a shape_input function to do the same for shapes.
\$\endgroup\$
1
  • \$\begingroup\$ Just use the latest version of the full code snippet for further reviews and suggestions \$\endgroup\$ Commented May 27, 2015 at 21:48

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.