Is My Pseudocode for a Python Calculator Good Enough?

0
11
Asked By CodeNinja27 On

I'm just getting started with programming, and I need some feedback on my pseudocode for a calculator. I'm curious to know if it's okay as it is or if there are areas where I can improve. Any tips would be greatly appreciated! Here's what I have so far:

BEGIN PROGRAM

# Starting off by setting up the numbers and the operators later on

import math

DECLARE Number One as Integer
DECLARE Number Two as Integer
DECLARE OPERATOR: STRING
DECLARE RESULT as REAL
OUTPUT "Welcome To The Calculator! Please Input Two Numbers And An Operator. If you do not wish to continue, input q"
IF User INPUT "q" THEN
OUTPUT "Have a good day!"
STOP

# Now that the user has chosen to continue they can input their number choices
IF USER INPUTS Number One
OUTPUT "Now select your second number" THEN
USER INPUTS Number Two
OUTPUT "Great! Now please select an operator from the following options: +, -, *, /"
IF OPERATOR = "+" THEN
DISPLAY Number One + Number Two
PRINT result
ELSE IF OPERATOR = "-" THEN
DISPLAY Number One - Number Two
Print result
ELSE IF OPERATOR = "*" THEN
DISPLAY Number One * Number Two
Print result
ELSE IF OPERATOR = "/" THEN
DISPLAY Number One / Number Two
Print result
IF USER INPUTS "0" IN "/"
OUTPUT "0 cannot be divided, please use a valid number"
ELSE IF OPERATOR IS INVALID
OUTPUT "Operation error, please select another operator listed"

4 Answers

Answered By ProgrammingPro123 On

I think your updated code is looking better! Make sure you're using consistent terminology too—try to stick to 'print' or 'display' instead of mixing both. It can get confusing for anyone reading your code.

Answered By LogicMaster94 On

Why not just write this directly in Python? It would be much more efficient and close to the pseudocode. But I understand if you want to manually go through it to familiarize yourself with the logic without auto-completions.

Answered By TechWhiz88 On

I noticed a flaw in your logic regarding how the program handles user input. When you check for 'q', that should be done before prompting the user for the first number. Instead of having the user input two numbers before validating, only prompt for the second after the first is entered.

Answered By DevGuru3000 On

Your structure is mostly on point, but you should initialize your variables properly. Also, instead of checking for division by zero separately, you could incorporate that check directly inside your division logic. For example, check if the operator is '/' and if the second number is 0 in that same block.

Related Questions

LEAVE A REPLY

Please enter your comment!
Please enter your name here

This site uses Akismet to reduce spam. Learn how your comment data is processed.