Python int/str checking
I have a homework assignment and I've done the bare minimum of the assignment, but while working on it, I became interested in how to make my program better. The point of the code is to draw a user defined shape and provide feedback if they enter invalid inputs. I want to go ahead and make it so if a user enters a str for the height/width or one of the coordinates it gives an error, but I am unable to figure out exactly how to do it. It's an intro class so if you'd keep in mind we're just getting to while loops and we've gone over for loops, if/else/etc.
Here's the code I currently have:
def draw_user_shape(pen):
shape = input("Please enter a shape: ")
while shape != "square" and shape != "rectangle" and shape != "triangle":
shape = input("Invalid shape, please enter either square, rectangle, or triangle: ")
if shape == "square":
h = input("Please enter a height: ")
while h开发者_运维百科 != int or int(h) < 1:
h = input("That is an invalid height, please enter an positive integer: ")
h = int(h)
c = input("Please enter a color: ")
while c != str and str(c) != "red" and str(c) != "green" and str(c) != "blue":
c = input("That is an invalid color, please enter either red, blue, or green: ")
c = str(c)
x = input("Please enter an x-coordinate: ")
while x != int and int(x) == 1:
x = input("That is an invalid x-coordinate, please enter an integer: ")
x = int(x)
y = input("Please enter a y-coordinate: ")
while y != int and int(y) == int:
y = input("That is an invalid y-coordinate, please enter an integer: ")
y = int(y)
pen.fillcolor(c)
pen.up()
pen.goto(x,y)
pen.down()
pen.begin_fill()
pen.goto(x,y+h)
pen.goto(x+h,y+h)
pen.goto(x+h,y)
pen.goto(x,y)
pen.end_fill()
pen.up()
elif shape == "rectangle":
h = input("Please enter a height: ")
while h != int and int(h) < 1:
h = input("That is an invalid height, please enter an positive integer: ")
h = int(h)
w = input("Please enter a width: ")
while w != int and int(w) < 1:
w = input("That is an invalid height, please enter an positive integer: ")
w = int(w)
c = input("Please enter a color: ")
while c != str and str(c) != "red" and str(c) != "green" and str(c) != "blue":
c = input("That is an invalid color, please enter either red, blue, or green: ")
c = str(c)
x = input("Please enter an x-coordinate: ")
while x != int and int(x) == 1:
x = input("That is an invalid x-coordinate, please enter an integer: ")
x = int(x)
y = input("Please enter a y-coordinate: ")
while y != int and int(y) == int:
y = input("That is an invalid y-coordinate, please enter an integer: ")
y = int(y)
pen.fillcolor(c)
pen.up()
pen.goto(x, y)
pen.down()
pen.begin_fill()
pen.goto(x,y+h)
pen.goto(x+w,y+h)
pen.goto(x+w,y)
pen.goto(x,y)
pen.end_fill()
pen.up()
elif shape == "triangle":
h = input("Please enter a height: ")
while h != int and int(h) < 1:
h = input("That is an invalid height, please enter an positive integer: ")
h = int(h)
w = input("Please enter a width: ")
while w != int and int(w) < 1:
w = input("That is an invalid height, please enter an positive integer: ")
w = int(w)
c = input("Please enter a color: ")
while c != str and str(c) != "red" and str(c) != "green" and str(c) != "blue":
c = input("That is an invalid color, please enter either red, blue, or green: ")
c = str(c)
x = input("Please enter an x-coordinate: ")
while x != int and int(x) == 1:
x = input("That is an invalid x-coordinate, please enter an integer: ")
x = int(x)
y = input("Please enter a y-coordinate: ")
while y != int and int(y) == int:
y = input("That is an invalid y-coordinate, please enter an integer: ")
y = int(y)
pen.fillcolor(c)
pen.up()
pen.goto(x,y)
pen.down()
pen.begin_fill()
pen.goto(x+w/2,y+h)
pen.goto(x+w,y)
pen.goto(x,y)
pen.end_fill()
pen.up()
def main():
import turtle
pen = turtle.Turtle()
draw_user_shape(pen)
main()
You'd want to use exception handling in this case. Basically the idea is to take the input and try to convert to an int
. If that fails, a ValueError
is raised. Otherwise you will get back the converted value. Remember, the input will always be given to you as a str
, so you can't simply test if it's an int
. Assuming you are using Python 3, you can do something like this to keep asking until the right value is typed in:
# keep asking for input until we get the right one
while True:
myInput = input('give me a number: ')
try:
myValue = int(myInput)
# if we reach this point, that means we got our number
break # this will jump out of the loop
except ValueError:
# if we reach this point, that means the input was bad
print('invalid input')
I would recommend using
not h.isdigit()
to check if the string, h, doesn't contain an integer. It won't work for floating point numbers because what it is really checking for is if each digit is in the 0-9 range and the .
won't be recognized (correctly) as a digit.
For instance the line
while h != int or int(h) < 1:
would become
while not h.isdigit() or int(h) < 1:
By the way, I'm assuming that you are using Python 3.x because otherwise your code wouldn't work because input
works differently in Python 2.x. In Python 3.x, it should always return a string, so there isn't any reason to check that the returned object is a string.
>>> isinstance('a', int)
False
>>> isinstance(2, int)
True
Use .isdigit()
or .isalnum()
to check , dependes that you prefer
Example
str_num= 'aaaa7777' str_num.isalnum() # true str_num.isdigit() # false
Example
str_num= 'aaaa 7777' # Note that has spaces str_num.isalnum() # false str_num.isdigit() # false
Example
str_num= '7777' str_num.isalnum() # True str_num.isdigit() # True
精彩评论