Python Forum
Calculator: several options, changing the number
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Calculator: several options, changing the number
#1
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
Reply
#2
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.
Reply
#3
Photo 
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.
Reply
#4
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.
Reply
#5
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.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  list all the different options that are filled in oog70 1 1,861 Sep-13-2019, 03:33 PM
Last Post: ichabod801
  python age calculator need to find the number of years before they turn 100 not using orangevalley 4 9,844 Mar-26-2018, 04:44 AM
Last Post: PyMan

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020