python伪代码例⼦_函数和操作数的Python伪代码
伪代码的基本思想是
a)使复杂代码易于理解,或
b)表达⼀个想法,即你将要编写代码/尚未想出如何编写代码。在
例如,如果我要制作⼀个需要从数据库中读取信息的⼯具,将其解析为字段,只获取⽤户请求的信息,然后格式化信息并将其打印到屏幕上,我的第⼀个代码草稿将是简单的伪代码,如下所⽰:# Header information
# Get user input
# Connect to Database
# Read in values from database
# Gather useful information
# Format information
# Print information
这为我的程序提供了⼀个基本的结构,这样我就不会迷失⽅向。另外,如果有⼈和我⼀起⼯作,我们可以把⼯作分成两部分(他负责编写连接到数据库的代码,⽽我负责编写代码以获取⽤户输⼊)
随着程序的进展,我将⽤真实的⼯作代码替换伪代码。在
^{pr2}$
在任何时候,我可能意识到代码中还有其他事情要做,如果我不想停⽌我正在做的⼯作,我会添加它们来提醒⾃⼰以后再回来编写代码。在# Header information #Don't forget to import the database dbconn class
user_input_row = int(input("Which row (1-10)? "))
#Protect against non-integer inputs so that the program doesn't fail
user_input_column = input("Which column (A, B, C)? "))
#Make sure the user gives a valid column before connecting to the database
python新手代码例子
dbase = dbconn("My_Database")
#Verify that we have a connection to the database and that the database is populated
row_of_interest = w(user_input_row)
# Separate the row by columns use .split()
# >> User only wants user_input_column
# Gather useful information
# Format information
# >> Make the table look like this:
# C C1 C2 < User's choice
# _________|________|_______
# Title | Field | Group
# Print information
在你完成编码之后,旧的伪代码甚⾄可以成为你的程序的好注释,这样其他⼈就可以马上知道你的程序的不同部分在做什么。在
当你不知道如何编写代码,但你知道你想要什么时,伪代码也能很好地⼯作,例如,如果你有⼀个关于如何在程序中创建某种循环的问题:my_list = [0,1,2,3,4,5]
for i in range(len(my_list)) but just when i is even:
print (my_list[i]) #How do I get it to print out when i is even?
伪代码可以帮助读者了解您正在尝试做什么,它们可以帮助您更轻松。在
在您的例⼦中,有⽤的伪代码(如解释代码的⽅式)可能如下所⽰:user_response=input("Input a number: ") # Get a number from user as a string
our_input=float(user_response) # Change that string into a float
def string (our_input):
if (our_input % 15) == 0 : # If our input is divisible by 15
return ("fizzbuzz")
elif (our_input % 3) == 0 : # If our input is divisible by 3 but not 15
return ("fizz")
elif (our_input % 5) == 0 : # If our input is divisible by 5 but not 15
return ("buzz")
else : # If our input is not divisible by 3, 5 or 15
return ("null")
print(string(our_input)) # Print out response

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。