Python Forum

Full Version: Calculator: several options, changing the number
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hi, I'm a beginner to Python and have been struggling with this assignment since yesterday: the user gives 2 numbers and then selects an action (1-6). If the selection is 5 then the user can change the given numbers and that's where I don't know how to proceed because the whole structure needs to be displayed again.

Here's my code:
print("Calculator")
def Loop():
	number1 = int(input("Give the first number: "))
	number2 = int(input("Give the second number: "))
	print("(1) +")
	print("(2) -")
	print("(3) *")
	print("(4) /")
	print("(5)Change numbers")
	print("(6)Quit")
	print("Current numbers: ",number1,number2)
	
	select = int(input("Please select something (1-6): "))
	if select == 5:
		Loop()
	if select == 1:
		print("The result is:", number1 + number2)	
	elif select == 2:
		print("The result is:", (number1 - number2))
	elif select == 3:
		print("The result is:", number1 * number2)
	elif select == 4:
		print("The result is:", number1 / number2)
	else:
		print("Selection was not correct.")
Loop()
When running this and selecting 5, the paragraphs "(1) + \n(2) -\n(3) *\n(4) /" and the rest are no longer shown
Hello and welcome to Python and the forums!
I don't exactly see what the issue is. In my case, if I select 5, I get asked for first and second number. And then options (1 +, 2 - ...) are being shown, just as on the first run.
Can you write the output you get, as well as output you would want to get? You can use [output] tags for that.
Thanks for the welcome, j.crater.

This is my output:
output


And this is what the whole thing should look like:
222


I'm running this code on viope, which is the portal I'm taking this course at. The inputs (100 & 25, 5, 10 & 30, 1, etc) are predefined.
Text on top of the window suggests the grader expects the function to return 0 upon completion. Try if this works:

    if select == 1:
        print("The result is:", number1 + number2)
        return 0
    elif select == 2:
        print("The result is:", (number1 - number2))
        return 0
    elif select == 3:
        print("The result is:", number1 * number2)
        return 0
    elif select == 4:
        print("The result is:", number1 / number2)
        return 0
Otherwise return is expected at a different point in program, which you should be able to figure from the task instructions.
I think you want line 16 to be an elif instead of an if. That way, when you get out of the recursive call to Loop triggered by choosing 5, it won't go to the else and say that there's an invalid selection.