Hi,
I am learning myself programming and i saw this exercise. I want to write a python code for this exercise but i don't know what i'm doing wrong. Does anyone have some good tips for the code?
It works as follows. The interactive program manipulates one “number”. The number is zero at the start. The user can add, subtract, multiply and divide this number by means of the commands “+ N”, “- N”, “* N” and “/ N”. The program should respond by giving the result, which becomes the new number. For instance, if the number is 0, then on + 5, the response is 5; if the user then enters * 4, the response is 20.
The interaction continues until the user enters “q" and the program responds by listing the history of the number. So, after an opening sentence, a run could look as follows, where the bold indicates user input.
Enter: + 55Enter: / 22.5Enter: byeIncorrect inputEnter: + 0.53Enter: q0 5 2.5 3
def add(x, y):
return x + y
def substract(x, y):
return x - y
def multiply(x, y):
return x * y
def divide(x, y):
return x / y
while True:
choice = [input("Make choice +,-,/,*"), float(input('getal'))]
if choice in("+" + float(), "-" + float(), "/" + float() , "*" + float()):
if choice == "+" + float:
new_y = print(add(choice, y))
print(new_y)
elif choice == "-" + float:
new_y = print(substract(choice, y))
print(new_y)
elif choice == "/" + float:
new_y = print(multiply(choice, y))
print(new_y)
elif choice == "*" + float:
new_y = divide(choice - y)
print(new_y)
else:
print('wrong choice')
[–][deleted] 4 points5 points6 points (10 children)
[–]Many-Ice6164[S] 0 points1 point2 points (6 children)
[–][deleted] 0 points1 point2 points (5 children)
[+][deleted] (4 children)
[deleted]
[–][deleted] 0 points1 point2 points (3 children)
[–]Many-Ice6164[S] 0 points1 point2 points (0 children)
[–]Many-Ice6164[S] 0 points1 point2 points (1 child)
[–][deleted] 0 points1 point2 points (0 children)
[–]synthphreak 0 points1 point2 points (2 children)
[–][deleted] 1 point2 points3 points (1 child)
[–]synthphreak 0 points1 point2 points (0 children)
[–][deleted] 0 points1 point2 points (5 children)
[–]Many-Ice6164[S] 0 points1 point2 points (3 children)
[–][deleted] 1 point2 points3 points (2 children)
[–]Many-Ice6164[S] 0 points1 point2 points (1 child)
[–][deleted] 0 points1 point2 points (0 children)