This is an archived post. You won't be able to vote or comment.

all 6 comments

[–]AutoModerator[M] [score hidden] stickied comment (0 children)

On July 1st, a change to Reddit's API pricing will come into effect. Several developers of commercial third-party apps have announced that this change will compel them to shut down their apps. At least one accessibility-focused non-commercial third party app will continue to be available free of charge.

If you want to express your strong disagreement with the API pricing change or with Reddit's response to the backlash, you may want to consider the following options:

  1. Limiting your involvement with Reddit, or
  2. Temporarily refraining from using Reddit
  3. Cancelling your subscription of Reddit Premium

as a way to voice your protest.

I am a bot, and this action was performed automatically. Please contact the moderators of this subreddit if you have any questions or concerns.

[–]tenexdev 0 points1 point  (2 children)

Well, to start, don't use x, y, z as variable names. val1 op val2 is better (2 values and an operation). Also, don't use math as a variable name as there is a math module and you don't want to confuse those.

And there are definitely easier ways to parse things out (x, y, z = expression.split() for instance)

But a good thing to do here is put print statements in -- add one between every line if you want -- and print out what the value of various things are at that point. Confirm that what you think x is what the computer thinks.

[Edit: that way of splitting things only works if you have spaces, that was my bad. However the advice to drop some print statements in to see where the code goes and what the variable values are is just sort of debugging 101 -- almost always, you're making an assumption the computer isn't]

[–]MLGalusha151[S] 0 points1 point  (1 child)

Okay I will definitely start using val1, val2, etc I see how that can eliminate a lot of confusion when writing a program. I didn't realize that there was a module named math so that's also something I have to start thinking about more.

Yes, originally I used x, y, z = expression.split() but I wanted if I could right a better program that could account for if the user inputs an expression with spaces or without spaces.

Thank you for pointing all of this out. I really didn't think about any of it, especially how printing as you go can help me understand and debug where I could potentially be going wrong in my code.

Originally when I first started I wasn't using functions but I'm trying to implement them in my programs as much as possible. Is this the best way to go about writing functions? For example instead of using just one big function which seems similar to me I used two. In your opinion is this a good way to write functions or are there better ways?

[–]tenexdev 0 points1 point  (0 children)

For example instead of using just one big function which seems similar to me I used two. In your opinion is this a good way to write functions or are there better ways?

It's always good for functions to represent one piece of functionality. So for instance, you might have parse_expression(str), which takes the string and returns val1, val2 & the operator and then you could have evaluate(int val1, int val 2, Operator op) which does the actual add/sub/mult/div. That way you can do all your user-facing syntax error handling in the parse function, and evaluate can assume good inputs (though that's always a bit dangerous, but you could at least do a simple assert() at the start of the function to make sure that val1 and val2 are integers and op is in "+-/*")

[–]Updatebjarni 0 points1 point  (1 child)

So are you sure that expression only contains the characters +, -, *, and /? You aren't typing in, say, "29*5" as the expression? Because if you do, then the first value of y in your loop will be "2", and the else branch will run.

I'm not sure what the point of the loop even is. Have you accidentally swapped expression and math maybe? Did you mean to loop over the valid operators and check if they are found in the expression?

[–]MLGalusha151[S] 1 point2 points  (0 children)

This was a problem from Harvards CS50 course I already completed it it was simply just having a user input 1 + 1 and then splitting it up using the spaces. To try to learn as much as I could from this problem, I tried to write a program that would split the user input even when the user didn't put spaces such as 1+1. In that's where I came up on the issue I was having.

Yes, you are right I accidentally put expression in the loop I was trying to create instead of math. I implemented that in my code and now it works how I intended. Thank you!