Python 可读性
Python readability
问候计算器,
我对这个网站和 python 都很陌生。
我编写了一个简单的程序来根据用户输入的数字创建一个设计者列表,该程序有效(据我所知)
但我很确定我的语法非常业余,所以我问你们中的流利者,我该怎么做才能做到这一点:
divisors = []
def devisors(x):
while True:
try:
for i in range(1,x+1):
if x%i==0:
divisors.append(i)
if x==i:
break
except ValueError, TypeError:
return "Invalid input."
continue
print "The list of divisors of",x,"Are: ", divisors
choice1= raw_input("Would you like to use devisors program?(Y/N): ")
while True:
try:
if choice1 in yes:
x = int(raw_input("Please enter a number: "))
devisors(x)
elif choice1 in no:
print "Alright, bye bye."
break
else:
print "invalid input"
choice1= raw_input("Would you like to use devisors program(Y/N): ")
continue
except ValueError, TypeError:
print "Invalid input, try again."
try:
choice2= raw_input("Would you like to try again?(Y/N): ")
if choice2 in yes:
divisors = []
continue
devisors(x)
elif choice2 in no:
print "Alright, bye bye. "
break
else:
print "invalid input"
choice2= raw_input("Would you like to try again?(Y/N): ")
continue
except ValueError, TypeError:
print "Invalid input, try again."
写得更好?
我希望你对这段代码发狂,告诉我(如果你愿意的话)可以做些什么来改进它,写更少的行,以及我犯的错误。
另外我不确定这是否在 Whosebug 上允许,如果不允许,请告诉我,我会删除它。
谢谢。
期待反馈。
首先,我们确实有专门的 Stack Exchange Code Review site,但您不是唯一一个在这里要求进行代码审查的人。
我注意到的第一件事是您正在努力将所有内容都放在一个方法中。
每当您遇到问题或任务时,您应该尝试将其分解为尽可能小的问题。之后我们就可以将这些小问题的解决方案实现为函数。
在您的示例中,您的任务如下:
- 获取用户输入的数字
- 得到那个数的除数
- 打印除数
- 询问用户是否想再试一次
让我们从列表的第二点开始:
def divisors_of(x):
if x < 0:
raise ValueError("Value cannot be smaller than 0")
divisors = []
for i in range(1, x + 1):
if x % i == 0:
divisors.append(i)
return divisors
看起来还不错,是吗?
在实现用户输入数字并打印结果后,我们已经得到了这样的结果:
try:
num = int(raw_input("Please enter a number: "))
divisors = divisors_of(num)
print "The divisors of ", num, " are: ", divisors
except (ValueError, TypeError) as ex:
print ex.message
查看我们的任务列表,我们仍然没有要求用户再次尝试。同样,恕我直言,如果我们将其逻辑放在一个函数中,它会更具可读性。这有两个额外的好处,我们可以将 yes/no 选择转换为布尔值并且编写的代码是 可重用 :
def yes_no_choice(text):
while True:
choice = raw_input(text)
if choice in "yes":
return True
# Returning like this is called early return
# As we jump out of the function, the code after
# the if is automatically the else statement
# The code is shorter and more readable
if choice in "no":
return False
print "Invalid answer"
我们的最终结果是这样的:
while True:
try:
num = int(raw_input("Please enter a number: "))
divisors = divisors_of(num)
print "The divisors of ", num, " are: ", divisors
except (ValueError, TypeError) as ex:
print ex.message
if not yes_no_choice("Would you like to try again?(Y/N): "):
print "Bye!"
break
# else we will loop
哦,如果没有必要,尽量不要使用全局变量。我在看你的 divisors = []
问候计算器, 我对这个网站和 python 都很陌生。 我编写了一个简单的程序来根据用户输入的数字创建一个设计者列表,该程序有效(据我所知)
但我很确定我的语法非常业余,所以我问你们中的流利者,我该怎么做才能做到这一点:
divisors = []
def devisors(x):
while True:
try:
for i in range(1,x+1):
if x%i==0:
divisors.append(i)
if x==i:
break
except ValueError, TypeError:
return "Invalid input."
continue
print "The list of divisors of",x,"Are: ", divisors
choice1= raw_input("Would you like to use devisors program?(Y/N): ")
while True:
try:
if choice1 in yes:
x = int(raw_input("Please enter a number: "))
devisors(x)
elif choice1 in no:
print "Alright, bye bye."
break
else:
print "invalid input"
choice1= raw_input("Would you like to use devisors program(Y/N): ")
continue
except ValueError, TypeError:
print "Invalid input, try again."
try:
choice2= raw_input("Would you like to try again?(Y/N): ")
if choice2 in yes:
divisors = []
continue
devisors(x)
elif choice2 in no:
print "Alright, bye bye. "
break
else:
print "invalid input"
choice2= raw_input("Would you like to try again?(Y/N): ")
continue
except ValueError, TypeError:
print "Invalid input, try again."
写得更好? 我希望你对这段代码发狂,告诉我(如果你愿意的话)可以做些什么来改进它,写更少的行,以及我犯的错误。
另外我不确定这是否在 Whosebug 上允许,如果不允许,请告诉我,我会删除它。
谢谢。 期待反馈。
首先,我们确实有专门的 Stack Exchange Code Review site,但您不是唯一一个在这里要求进行代码审查的人。
我注意到的第一件事是您正在努力将所有内容都放在一个方法中。
每当您遇到问题或任务时,您应该尝试将其分解为尽可能小的问题。之后我们就可以将这些小问题的解决方案实现为函数。
在您的示例中,您的任务如下:
- 获取用户输入的数字
- 得到那个数的除数
- 打印除数
- 询问用户是否想再试一次
让我们从列表的第二点开始:
def divisors_of(x):
if x < 0:
raise ValueError("Value cannot be smaller than 0")
divisors = []
for i in range(1, x + 1):
if x % i == 0:
divisors.append(i)
return divisors
看起来还不错,是吗?
在实现用户输入数字并打印结果后,我们已经得到了这样的结果:
try:
num = int(raw_input("Please enter a number: "))
divisors = divisors_of(num)
print "The divisors of ", num, " are: ", divisors
except (ValueError, TypeError) as ex:
print ex.message
查看我们的任务列表,我们仍然没有要求用户再次尝试。同样,恕我直言,如果我们将其逻辑放在一个函数中,它会更具可读性。这有两个额外的好处,我们可以将 yes/no 选择转换为布尔值并且编写的代码是 可重用 :
def yes_no_choice(text):
while True:
choice = raw_input(text)
if choice in "yes":
return True
# Returning like this is called early return
# As we jump out of the function, the code after
# the if is automatically the else statement
# The code is shorter and more readable
if choice in "no":
return False
print "Invalid answer"
我们的最终结果是这样的:
while True:
try:
num = int(raw_input("Please enter a number: "))
divisors = divisors_of(num)
print "The divisors of ", num, " are: ", divisors
except (ValueError, TypeError) as ex:
print ex.message
if not yes_no_choice("Would you like to try again?(Y/N): "):
print "Bye!"
break
# else we will loop
哦,如果没有必要,尽量不要使用全局变量。我在看你的 divisors = []