Python Tasks - Pseudocode

 

Python Tasks - Pseudocode, Code and Explanations

What is pseudocode?
Pseudocode is a method of planning which enables the programmer to plan without worrying about syntax.

Task 1: Write a program that asks the user for a temperature in Fahrenheit and prints out the same temperature in Celsius.

Example Pseudocode:
x = Get user input
x = Convert X to Celsius
Output message displaying Celsius temperature

Example code:

The first part of the program rounds gets the precise temperature, contrary to the second program which uses the round function to round the result to the nearest whole number. The round function works as shown below:

round(x,y)

"x" is what you're rounding and "y" is how many decimal places you want to round up to.

Task 2: Write a program that converts from Fahrenheit to Celsius or from Celsius to Fahrenheit, depending on the user's choice.

Pseudocode:
x = input "Press 1 to convert from Fahrenheit to Celsius or Press 2 to convert from Celsius to Fahrenheit. "
y = input ask what number?

if 1 is pressed
do f to c conversion
print output

elif 2 is pressed
do c to f conversion
print output

else
print "Please enter either 1 or 2"

Actual code:


Code w/ while loop:
while True: 
    choice = input("Press 1 to convert from Fahrenheit to Celsius or Press 2 to convert from Celsius to Fahrenheit. ")

    if float(choice)==1:
        temp = input("What is the number that you would like to convert? ")
        temp = (float(temp) - 32) * 5 / 9
        print ("Your temperature in Celsius is: %s." %temp)
        break #stops while loop
    elif float(choice)==2:
        temp = input("What is the number that you would like to convert? ")
        temp = float(temp) * 9/5 + 32
        print ("Your temperature in Fahrenheit is: %s." %temp)
        break #stops while loop
    else: #only if what ever is entered is a number and not 1 or 2
        print ("") #linebreak to make it look better
        print ("PLEASE ENTER EITHER 1 OR 2!")
        print ("") #linebreak to make it look better
#code repeats until either 1 or 2 is entered

Screenshot of the code in action:

Since the code above could not handle if characters are included, the feature is added in the code below:
while True:
    try:
        choice = input("Press 1 to convert from Fahrenheit to Celsius or Press 2 to convert from Celsius to Fahrenheit. ")


        if float(choice)==1:
            temp = input("What is the number that you would like to convert? ")
            temp = (float(temp) - 32) * 5 / 9
            print ("Your temperature in Celsius is: %s." %temp)
            break
        elif float(choice)==2:
            temp = input("What is the number that you would like to convert? ")
            temp = float(temp) * 9/5 + 32
            print ("Your temperature in Fahrenheit is: %s." %temp)
            break
        else:
            print ("")
            print ("PLEASE ENTER EITHER 1 OR 2!")
            print ("")
            
    except ValueError:
        print ("")
        print ("PLEASE ENTER EITHER 1 OR 2!")
        print ("")

Updated Code:

Updated code (again):
while True:
    
    try:
        
        choice = input("Press 1 to convert from Fahrenheit to Celsius or Press 2 to convert from Celsius to Fahrenheit, Press 3 to stop the program. ")

        if float(choice)==3:
            break
        else:
            print ("")
            temp = input("What is the number that you would like to convert? ")
            if float(choice)==1:
                temp = (float(temp) - 32) * 5 / 9
                print ("Your temperature in Celsius is: %s C." %temp)
            
            elif float(choice)==2:
                temp = float(temp) * 9/5 + 32
                print ("Your temperature in Fahrenheit is: %s F." %temp)

            else:
                print ("PLEASE ENTER EITHER 1 OR 2 OR 3!")

        print ("")
            
    except ValueError:
        print("")
        print ("INVALID INPUT! TRY AGAIN")
        print("")

Task 3: Write a program that lets the user type words and when they press 'x', it prints how many words the user inputted then quits program.

Pseudocode:
while true
ask user to enter a word
print word
if word = x
print number of words entered
break

count = 0 #is outside of the while loop because if it wasn't, count would always reset to 0
while True:
    word = input("Please enter a word (x to quit): ")
    print (word)       
    if word=="x":
        print ("You have entered %s words." %count)
        break #x does not count as a word
    else:
        count = count + 1 #adds 1 to count each time a word is entered

Task 4: Write a program in which a password is set and the program will keep prompting the user to guess it, until they get the word right.

Pseudocode:
set password
ask user to try to guess password
if the password is wrong, say access denied
if the password is right, grant access

password = ("sesame")
hello = input("Enter the password: ")
while hello != password:
    print ("Access denied")
    hello = input("Enter the password: ")
    if hello == password:
        print ("Login successful!")

Task 5: Write a program that asks for a user to enter a number, and then print all those numbers after 10 numbers are entered.

Pseudocode:
numbercount is 0
list
while the number count <= 9
ask for input
add one to number count
add number to list
print numbers on a separate line each time

In this task, I created a list which stored the numbers. When 10 numbers are entered, it prints all of those numbers.

numcount = 0
lis = []
while numcount <= 9: #because lists start counting from 0
    number=input("Enter a number: ")
    numcount = numcount + 1
    lis.append(number)
print ("The numbers you entered were: ")
print ("\n".join(lis))

The last line lets me print all the numbers on new lines each time