PROBLEM SET 1: MATH INTERPRETER | SOLUTION (CS50 PYTHON)

preview_player
Показать описание


––– DISCLAIMER –––

The following videos are for educational purposes only. Cheating or any other activities are highly discouraged!! Using another person’s code breaks the academic honesty guidelines. This solution is for those who have finished the problem sets and want to watch for educational purposes, learning experience, and exploring alternative ways to approach problems and is NOT meant for those actively doing the problem sets. All problem sets presented in this video are owned by Harvard University.

–––
Рекомендации по теме
Комментарии
Автор

This problem is actually easier when you understand what a split does, I couldn’t figure it out because I didn’t know what it meant or how to implement it so thank you

moeletsimelamu
Автор

great video, I did not know that the variable "result" could be made repeatedly this way in conditionals. My solution was:

def main():
exp = input("Expression: ")
x, y, z = exp.split(" ")
xf = float(x)
zf = float(z)

if y == "+":
print (round((xf + zf), 1 ))
elif y == "-":
print (round((xf - zf), 1 ))
elif y == "*":
print (round((xf*zf), 1 ))
else:
print (round((xf/zf), 1 ))

main()

damian
Автор

My solution:

# Get the expression
x, y, z = input("Expression: ").strip().split()

# Change type of variable x and z
x = float(x)
z = float(z)

# Perform the operations
match y:
case "+":
print(x + z)
case "-":
print(x - z)
case "/":
print(x / z)
case "*":
print(x * z)

MuhammadIbrahim-mghx
Автор

Perfect! I could NOT work out how to solve this problem but now it is all clear. Thank-you.

johncheriton
Автор

thank you for the new perspective. love how your way is so easy to follow!

def main():
expression = input("Expression: ")

x = float(expression.split()[0])
y = expression.split()[1]
z = float(expression.split()[2])

if y == "+":
print(x + z)
elif y == "-":
print(x - z)
elif y == "*":
print(x * z)
elif y == "/":
print(x / z)

main()

punkguy
Автор

my approach is using the eval() function which evaluates the expression, this saved me many lines of code and passed the test
x=input("what's x? ")

result=float(eval(x))
print(result)

EasyBeezy-coding
Автор

m = input("enter an arithmatic expression ")

print(round(float(eval(m)), 1))
This works too!

junemiabba
Автор

ah... there is a bug in the code in the division section. There needs to be a condition to check the Z!=0 if y is '/'. It is specified as a condition in description it self.

For instance, if the user inputs 1 + 1, your program should output 2.0. Assume that, if y is /, then z will not be 0.

Using a match case should take care of this.

if y == '+' or y == '-' or y == '*':

match y:
code the conditions

elif y == '/' and z != 0:
match y:
case '/':
ans = float(x)/float(z)

hitechpanchal
Автор

Thank you, I was a bit confused with the split method, you brightened my day up

muratozdemir
Автор

I didn't know how to apply split() for this problem. Thank you.

studywithshin
Автор

This is how i solved it. Hope it helps

expression = input("Expression: ").casefold().strip()
x, y, z = expression.split(" ")

num1 = int(x)
num2 = int(z)

add = num1 + num2
subtract = num1 - num2
divide = num1 / num2
multiply = num1 * num2

if "+" in expression:
print(float(add))

elif "-" in expression:
print(float(subtract))

elif "/" in expression:
print(float(divide))

elif "*" in expression:
print(float(multiply))

else:
print("Error: Missing expression")

I think there are some unnecessary lines but it passed all checks

Noah-lklb
Автор

Exactly what I needed. Thank you so much

khunter
Автор

.strip() gets rid of the spaces at the start and end of the input. but what if the input has spaces inbetween. what if the user input 1+1 with no spaces, instead of 1 + 1 like the problem offers? I tried .remove(" ", "") but then it doesn't do the split correctly and errors.

tejni
Автор

thank u for this! i'm new to python and coding in general and i used the "if "+" in y print (x + y) method haha. it worked just fine but it was definitely beneficial to see it done this way, it makes more sense lol<3

ophelia
Автор

I got the basic code right but had a hard time printing because I made a def for it and returned for example x + y instead of just printing it.
This one was the hardest for me in problem set 1

yuyum
Автор

You got the right results to pass the check but it says in the instructions x and z need to be integers. Later on when getting the answer is when they get converted to a float.

TrueJamo
Автор

I did something similar however not quite the same. I went with the approach of rather than calculating for result, I put print(new_x + new_y) of course replacing the operator as required.

I also used if, Élif and else since they only required the 4 operators. However if this were to be more operators, your approach of only if makes a lot more sense

mehdisheriff
Автор

thank you for the detailed explanation!!
my solutions looks like that:

x, y, z = input("arithmetic expression: ").split(" ")

if y == "+":
print(float(x) + float(z))
elif y == "-":
print(float(x) - float(z))
elif y == "*":
print(float(x) * float(z))
elif y == "/":
print(float(x) / float(z))

senisneru
Автор

That is very very good vidio
Thanks bot this vidio🎉

mohamadmovahed-dpjo
Автор

very challenging ques, split holded me down soo bad😂😂😂

tanishq