我对PseudoCode的概念很陌生……我想知道如何在PseudoCode中编写函数和操作数,如模数、底除法等。为这段代码编写PseudoCode实际上可能会帮助我更好地理解...
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))发布于 2017-08-18 08:05:06
假设%作为模运算的知识,你的代码并不难理解。Floor division真的可以写成一个常规的分区。如果真的有必要的话,将结果放在地板上。
如果您不知道如何表达它们,那么显式地编写modulus(x, y)或floor(z)。
伪代码应该是可读的,而不是复杂的。
伪代码甚至可能只是单词,而不是“代码”。它的伪部分来自于操作的逻辑表达式
发布于 2017-08-18 08:30:41
PseudoCode的基本思想是
a)使复杂的代码易于理解,或者
b)表达你将要编码/还没有想好如何编码的想法。
例如,如果我要制作一个工具,需要从数据库中读取信息,将其解析为字段,仅获取用户请求的信息,然后格式化信息并将其打印到屏幕上,则我的第一个代码草案将是简单的PseudoCode:
# Header information
# Get user input
# Connect to Database
# Read in values from database
# Gather useful information
# Format information
# Print information这为我的程序提供了一个基本的结构,这样我就不会在制作过程中迷失方向。此外,如果其他人与我一起工作,我们可以分担工作(他负责连接到数据库的代码,而我负责代码以获得用户输入)。
随着程序的进展,我将用实际的、有效的代码替换PseudoCode。
# 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在您完成编码之后,旧的PseudoCode甚至可以作为对您的程序的良好注释,这样另一个人就可以立即知道您的程序的不同部分在做什么。
当你不知道如何编写代码,但你知道自己想要什么时,PseudoCode也能很好地解决问题,例如,如果你有一个关于如何在程序中进行某种循环的问题:
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?PseudoCode帮助读者知道你想要做什么,他们可以更容易地帮助你。
在您的例子中,用于解释代码等事情的有用PseudoCode可能如下所示:
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发布于 2017-08-19 08:29:35
感谢大家的意见,从我所读到的,我想出了这个,如果你认为有任何地方需要调整,请让我知道。再次感谢
使用PYTHON的函数
User_response=input(“输入数字:")
our_input=float(user_response)
定义字符串(our_input):
if (our_input % 15) == 0 :
return ("fizzbuzz")
elif (our_input % 3) == 0 :
return ("fizz")
elif (our_input % 5) == 0 :
return ("buzz")print(string(our_input))
<<
这是伪代码
请求用户输入
确保输入为数字
如果输入可以被15整除
将"fizzbuzz“发送到main
程序
但是如果输入可以被3整除而不是15
向主程序发送"fizz“
但是如果输入可以被5整除,而不是15或3
向主程序发送"buzz“
显示结果。
https://stackoverflow.com/questions/45746585
复制相似问题