![](/img/trans.png)
[英]Why does my python code not continue execution once my function "executelogin()" has been executed?
[英]Why does my code continue?
該代碼應采用用戶輸入的形狀類型。 然后輸入高度,然后打印由*組成的形狀(目前為三角形/正方形)
該代碼將一直起作用,直到用戶鍵入錯誤的輸入才能啟動,然后輸入正確的函數名稱並嘗試將其關閉。 任何想法如何解決它?
錯誤:
Enter shape to draw (q to quit): sad
Unknown shape. Please try again
Enter shape to draw (q to quit): triangle
Enter size: 6
*
**
***
****
*****
******
Enter shape to draw (q to quit): q
Goodbye
Enter size: #this should not be there
整個代碼是““”喬丹·漢普森的形狀程序“”“
def main():
"""main boss function"""
shape = input("Enter shape to draw (q to quit): ").lower()
if shape == "q":
print("Goodbye")
return
else:
get_valid_shape(shape)
call_shape(shape)
main()
def call_shape(shape):
"""CALLS THE shape"""
size = int(input("Enter size: "))
get_valid_size(size)
if shape == "square":
return print_square(size)
elif shape == "triangle":
return print_triangle(size)
else:
return main()
def get_valid_size(size):
"""checks to see if the size is valid"""
if size <= 0:
print("Value must be at least 1")
main()
def get_valid_shape(shape):
"""gets a valid shape"""
shape_1 = shape
shapes = ["square", "triangle", "q"]
while shape_1 not in shapes:
print("Unknown shape. Please try again")
return main()
def print_square(size):
"""prints a square from the input"""
for _ in range(0, size):
print(size * "*")
def print_triangle(size):
"""prints a triangle in *'s"""
for _ in range(1, size +1):
print((size -(size -_)) * "*")
main()
這是由於您的函數get_valid_shape(),函數調用函數fonmain(),所以當您輸入“ sad”時,您將啟動一個新的main(),但在函數get_valid_shape()中(在主過程中,您仍然處在get_valid_shape(shape)
行中) get_valid_shape(shape)
)。 當您按“ q”時,您退出此行並傳遞給call_shape(shape)
,上一個形狀是三角形,因此它要求您輸入尺寸。
為了避免這種情況,我建議使用此代碼
def main():
shape = ""
while shape != "q":
"""main boss function"""
shape = "" #reinitialize
shape = input("Enter shape to draw (q to quit): ").lower()
if shape == "q":
print("Goodbye")
elif get_valid_shape(shape) :
call_shape(shape)
else:
print("Unknown shape. Please try again")
def call_shape(shape):
"""CALLS THE shape"""
size = int(input("Enter size: "))#ask a 1st time withour print error message
while get_valid_size(size): #while size is false, ask again
size = int(input("Enter size: "))
if shape == "square":
return print_square(size)
elif shape == "triangle":
return print_triangle(size)
def get_valid_size(size):
"""checks to see if the size is valid"""
if size <= 0:
print("Value must be at least 1")
return True #return true to loop at the while
else :
return False #return false to break the while
def get_valid_shape(shape):
"""gets a valid shape"""
shape_1 = shape
shapes = ["square", "triangle", "q"]
if shape_1 not in shapes:
return False
else :
return True
def print_square(size):
"""prints a square from the input"""
for _ in range(0, size):
print(size * "*")
def print_triangle(size):
"""prints a triangle in *'s"""
for _ in range(1, size +1):
print((size -(size -_)) * "*")
main()
我建議您使用python中的pdb模塊。 這是一個調試模塊非常有用的(你可以看到如何一步運行algorithme一步,去什么地方,到一個函數等) 鏈接
您以錯誤的方式使用了這些return
,您實際上並不需要任何return值,因為您在代碼中沒有做任何事情!
def main():
"""main boss function"""
shape = input("Enter shape to draw (q to quit): ").lower()
if shape == "q":
print("Goodbye")
else:
get_valid_shape(shape)
call_shape(shape)
main()
def call_shape(shape):
"""CALLS THE shape"""
size = int(input("Enter size: "))
get_valid_size(size)
if shape == "square":
print_square(size)
elif shape == "triangle":
print_triangle(size)
else:
main()
def get_valid_size(size):
"""checks to see if the size is valid"""
if size <= 0:
print("Value must be at least 1")
main()
def get_valid_shape(shape):
"""gets a valid shape"""
while shape not in ["square", "triangle", "q"]:
print("Unknown shape. Please try again")
main()
def print_square(size):
"""prints a square from the input"""
for _ in range(0, size):
print(size * "*")
def print_triangle(size):
"""prints a triangle in *'s"""
for _ in range(1, size +1):
print((size -(size -_)) * "*")
main()
在我看來,這是一個糟糕的代碼,因為您使用了無用的函數,而無需使用它們就可以編寫整個代碼。
inProgram = True
while inProgram:
shape = raw_input("Enter shape to draw (q to quit): ").lower()
while shape not in ["square", "triangle", "q"]:
shape = raw_input("Unknown shape. Please try again: ").lower()
if shape == "q":
print("Goodbye")
inProgram=False
else:
size = raw_input("Enter size: ")
while not size.isdigit() or size<=0:
size = raw_input("Value must be at least 1\nTry again: ")
size = int(size)
if shape == "square":
for _ in range(0, size):
print(size * "*")
else:
for _ in range(1, size +1):
print((size -(size -_)) * "*")
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.