Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/332.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python-函数和操作数的伪代码_Python_Function_Pseudocode_Operands - Fatal编程技术网

Python-函数和操作数的伪代码

Python-函数和操作数的伪代码,python,function,pseudocode,operands,Python,Function,Pseudocode,Operands,我对伪代码的概念很陌生。。。我想了解如何用伪代码编写函数和操作数,如模、底除法等。为这段代码编写伪代码实际上可以帮助我更好地理解 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

我对伪代码的概念很陌生。。。我想了解如何用伪代码编写函数和操作数,如模、底除法等。为这段代码编写伪代码实际上可以帮助我更好地理解

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))

假设将
%
的知识作为模运算,您的代码实际上并不难理解。楼层划分实际上可以写成常规划分。如果真的有必要,请说明结果

如果您不知道如何表达它们,那么显式地编写
模(x,y)
地板(z)

伪代码应该是可读的,而不是复杂的


伪代码甚至可能只是文字,而不是“代码”。它的伪部分来自于运算的逻辑表达式

假设将
%
的知识作为模运算,您的代码实际上并不难理解。楼层划分实际上可以写成常规划分。如果真的有必要,请说明结果

如果您不知道如何表达它们,那么显式地编写
模(x,y)
地板(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
伪代码可以帮助读者了解您正在尝试做什么,并且可以更轻松地帮助您


在您的情况下,有用的伪代码(如解释代码的方式)可能如下所示:

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

伪代码的基本思想是

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
伪代码可以帮助读者了解您正在尝试做什么,并且可以更轻松地帮助您


在您的情况下,有用的伪代码(如解释代码的方式)可能如下所示:

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

感谢大家的投入,从我所读到的,我想到了这个,如果有任何地方你认为需要调整,请让我知道。 再次感谢

该函数使用PYTHON 用户响应=输入(“输入一个数字:”)

我们的输入=浮动(用户响应)

def字符串(我们的_输入):

打印(字符串(我们的输入))

这是伪代码

请求用户输入

确保输入是一个数字

如果输入可以被15整除

将“嘶嘶声”发送到主屏幕

节目

但是如果输入可以被3整除,而不是15整除

向主程序发送“fizz”

但是如果输入可以被5整除,而不是15或3

向主程序发送“嗡嗡声”


显示结果。

感谢大家的投入,从我所读到的,我想到了这个,如果你认为有任何地方需要调整,请告诉我。 再次感谢

该函数使用PYTHON 用户响应=输入(“输入一个数字:”)

我们的输入=浮动(用户响应)

def字符串(我们的_输入):

打印(字符串(我们的输入))

这是伪代码

请求用户输入

确保输入是一个数字

如果输入可以被15整除

将“嘶嘶声”发送到主屏幕

节目

但是如果输入可以被3整除,而不是15整除

向主程序发送“fizz”

但是如果输入可以被5整除,而不是15或3

向主程序发送“嗡嗡声”


显示结果。

顺便说一下,if语句或return语句不需要括号values@cricket_007此外,为了清楚起见,顺便说一句,if语句或return不需要括号values@cricket_007除此之外,为了清楚起见