Developer Emi
Developer Emi

Reputation: 1

Python - PseudoCode for functions and operands

I am quite new to the PseudoCode concept... I would like to get an idea of how functions and operands like modulus, floor division and the likes would be written in PseudoCode. Writing the PseudoCode for this code might actually help me understand better...

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

Upvotes: 0

Views: 6521

Answers (3)

Developer Emi
Developer Emi

Reputation: 1

THANKS GUYS FOR ALL OF YOUR INPUT, FROM ALL I READ, I CAME UP WITH THIS, IF THERE ARE ANY AREAS U THINK NEEDS TO BE ADJUSTED PLEASE LET ME KNOW. THANKS AGAIN

THE FUNCTION USING PYTHON

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

print(string(our_input))

<<------------------------------->>

IT'S PSEUDOCODE

Request an input from the user

Ensure that the input is a number

If the input is divisible by 15

send "fizzbuzz" to the main

program

But if the input is divisible by 3 and not 15

send "fizz" to the main program

But if the input is divisible by 5 and not 15 or 3

send "buzz" to the main program

Display the result.

Upvotes: 0

Davy M
Davy M

Reputation: 1696

The basic idea of PseudoCode is either to

a) Make complicated code understandable, or

b) Express an idea that you are going to code/haven't yet figured out how to code.

For example, if I am going to make a tool that needs to read information in from a database, parse it into fields, get just the info that the user requests, then format the information and print it to the screen, my first draft of the code would be simple PseudoCode as so:

# Header information

# Get user input

# Connect to Database

# Read in values from database

# Gather useful information

# Format information

# Print information

This gives me a basic structure for my program, that way I don't get lost as I'm making it. Also, if someone else is working with me, we can divvy up the work (He works on the code to connect to the database, and I work on the code to get the user input.)

As the program progresses, I would be replacing PseudoCode with real, working code.

# 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

At any point I might realize there are other things to do in the code, and if I don't want to stop what I'm working on, I add them in to remind myself to come back and code them later.

# 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

After you're done coding, the old PseudoCode can even serve to be good comments to your program so that another person will know right away what the different parts of your program are doing.

PseudoCode also works really well when asking a question when you don't know how to code something but you know what you want, for example if you had a question about how to make a certain kind of loop in your program:

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?

The PseudoCode helps the reader know what you're trying to do and they can help you easier.


In your case, useful PseudoCode for things like explaining your way through code might look like:

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

Upvotes: 0

OneCricketeer
OneCricketeer

Reputation: 191701

Your code really isn't that hard to understand assuming the knowledge of % as the modulus operation. Floor division can really just be written as a regular division. Floor the result, if really necessary.

If you don't know how to express them, then explicitly write modulus(x, y) or floor(z).

Pseudocode is meant to be readable, not complicated.

Pseudocode could even be just words, and not "code". The pseudo part of it comes from the logical expression of operations

Upvotes: 2

Related Questions