功能不断要求输入
function keeps asking for input
我编写的函数绘制了 square
、triangle
或 rectangle
。它要求用户输入他们想要绘制的形状。如果输入不是以下之一,它会重新要求用户输入有效的输入:
有效输入
square,
triangle,
rectangle,
q #function Quits.
我遇到的问题是,当用户第一次键入有效输入时,该函数会完美运行并绘制该形状。但是,如果用户首先输入 "invalid input",例如(圆圈),它会要求用户重新输入 "valid input(shape)"。当他这样做时,无穷大函数一直在说:
错误信息
Unknown shape. Please try again
Enter shape to draw (q to quit):
代码
def get_valid_shape():
''' Asking the user to enter a valid shape that the function can draw '''
shape = input("Enter shape to draw (q to quit): ").lower()
unvalid_names1 = shape != "triangle" and shape != "square"
unvalid_names2 = shape != "rectangle" and shape != "q"
while unvalid_names1 == True and unvalid_names2 == True:
print("Unknown shape. Please try again")
shape = input("Enter shape to draw (q to quit): ").lower()
if shape == "q":
print("Goodbye")
return
有人能帮忙吗?
这是因为您在没有条件跳出循环的情况下要求循环内的下一个值。一个简单的修复方法是:
def get_valid_shape():
''' Asking the user to enter a valid shape that the function can draw '''
shape = input("Enter shape to draw (q to quit): ").lower()
unvalid_names1 = shape != "triangle" and shape != "square"
unvalid_names2 = shape != "rectangle" and shape != "q"
quit = False
while not quit and unvalid_names1 == True and unvalid_names2 == True:
print("Unknown shape. Please try again")
shape = input("Enter shape to draw (q to quit): ").lower()
quit = shape == 'q'
if shape == "q":
print("Goodbye")
return
get_valid_shape()
除此之外,您的代码很难理解。我冒昧地制作了另一个版本。可读性确实是意见,但它也解决了您的问题,同时使(恕我直言)更清楚您要做什么:
def get_valid_shape():
''' Asking the user to enter a valid shape that the function can draw '''
valid_shapes = set(['triangle', 'square', 'rectangle'])
user_quit = False
requested_shape = ''
while True:
requested_shape = input("Enter shape to draw (q to quit): ").lower()
if requested_shape in valid_shapes:
break
elif requested_shape == 'q':
user_quit = True
break
else:
print('Invalid shape')
if not user_quit:
print('Would now print shape')
else:
print('Goodbye')
get_valid_shape()
我编写的函数绘制了 square
、triangle
或 rectangle
。它要求用户输入他们想要绘制的形状。如果输入不是以下之一,它会重新要求用户输入有效的输入:
有效输入
square, triangle, rectangle, q #function Quits.
我遇到的问题是,当用户第一次键入有效输入时,该函数会完美运行并绘制该形状。但是,如果用户首先输入 "invalid input",例如(圆圈),它会要求用户重新输入 "valid input(shape)"。当他这样做时,无穷大函数一直在说:
错误信息
Unknown shape. Please try again Enter shape to draw (q to quit):
代码
def get_valid_shape():
''' Asking the user to enter a valid shape that the function can draw '''
shape = input("Enter shape to draw (q to quit): ").lower()
unvalid_names1 = shape != "triangle" and shape != "square"
unvalid_names2 = shape != "rectangle" and shape != "q"
while unvalid_names1 == True and unvalid_names2 == True:
print("Unknown shape. Please try again")
shape = input("Enter shape to draw (q to quit): ").lower()
if shape == "q":
print("Goodbye")
return
有人能帮忙吗?
这是因为您在没有条件跳出循环的情况下要求循环内的下一个值。一个简单的修复方法是:
def get_valid_shape():
''' Asking the user to enter a valid shape that the function can draw '''
shape = input("Enter shape to draw (q to quit): ").lower()
unvalid_names1 = shape != "triangle" and shape != "square"
unvalid_names2 = shape != "rectangle" and shape != "q"
quit = False
while not quit and unvalid_names1 == True and unvalid_names2 == True:
print("Unknown shape. Please try again")
shape = input("Enter shape to draw (q to quit): ").lower()
quit = shape == 'q'
if shape == "q":
print("Goodbye")
return
get_valid_shape()
除此之外,您的代码很难理解。我冒昧地制作了另一个版本。可读性确实是意见,但它也解决了您的问题,同时使(恕我直言)更清楚您要做什么:
def get_valid_shape():
''' Asking the user to enter a valid shape that the function can draw '''
valid_shapes = set(['triangle', 'square', 'rectangle'])
user_quit = False
requested_shape = ''
while True:
requested_shape = input("Enter shape to draw (q to quit): ").lower()
if requested_shape in valid_shapes:
break
elif requested_shape == 'q':
user_quit = True
break
else:
print('Invalid shape')
if not user_quit:
print('Would now print shape')
else:
print('Goodbye')
get_valid_shape()