我对伪代码概念很陌生...我想了解一下如何在伪代码中编写模数、楼层划分等函数和操作数。为这段代码编写伪代码实际上可以帮助我更好地理解......
user_response=input("Input a number: ")
our_input=float(user_response)
def string (our_input):
if (our_input % 15) == 0 :
return ("fizzbuzz")
elif (our_input % 3) == 0 :
return ("fizz")
elif (our_input % 5) == 0 :
return ("buzz")
else :
return ("null")
print(string(our_input))
假设%
的知识作为模运算,你的代码真的不难理解。楼层划分实际上可以写成常规划分。如果确实有必要,请对结果进行地板处理。
如果你不知道如何表达它们,那么明确写modulus(x, y)
或floor(z)
.
伪代码应该是可读的,而不是复杂的。
伪代码甚至可能只是单词,而不是"代码"。它的伪部分来自操作的逻辑表达式
伪代码的基本思想是
a( 使复杂的代码易于理解,或
b(表达一个你打算编码/还没有弄清楚如何编码的想法。
例如,如果我要制作一个工具,需要从数据库中读取信息,将其解析为字段,仅获取用户请求的信息,然后格式化信息并将其打印到屏幕上,那么我的代码初稿将是简单的伪代码,如下所示:
# Header information
# Get user input
# Connect to Database
# Read in values from database
# Gather useful information
# Format information
# Print information
这为我的程序提供了一个基本结构,这样我就不会在制作它时迷失方向。此外,如果其他人正在与我合作,我们可以分配工作(他处理代码以连接到数据库,我处理代码以获取用户输入。
随着程序的进展,我将用真正的工作代码替换伪代码。
# Header information
user_input_row = int(input("Which row (1-10)? "))
user_input_column = input("Which column (A, B, C)? "))
dbase = dbconn("My_Database")
row_of_interest = dbase.getrow(user_input_row)
# Gather useful information
# Format information
# Print information
在任何时候,我都可能意识到代码中还有其他事情要做,如果我不想停止我正在做的事情,我会添加它们以提醒自己稍后再回来编写代码。
# 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
dbase = dbconn("My_Database")
#Verify that we have a connection to the database and that the database is populated
row_of_interest = dbase.getrow(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
感谢大家的所有意见,从我阅读的所有内容中,我想出了这个,如果您认为有任何需要调整的地方,请告诉我。 再次感谢
使用蟒蛇的函数
user_response=输入("输入一个数字: "(
our_input=浮点(user_response(
def 字符串 (our_input(:
if (our_input % 15) == 0 :
return ("fizzbuzz")
elif (our_input % 3) == 0 :
return ("fizz")
elif (our_input % 5) == 0 :
return ("buzz")
打印(字符串(our_input((
<<------------------------------->>
这是伪代码
请求用户输入
确保输入为数字
如果输入可被 15 整除
将"嘶嘶声"发送到主
程序
但是如果输入可以被 3 而不是 15 整除
将"嘶嘶声"发送到主程序
但是,如果输入可以被 5 整除,而不是 15 或 3
向主程序发送"嗡嗡声">
显示结果。