Python Calculator: Several options, changing the number

Question:

Hello I am new to python and I am trying to do an assignment but I couldn’t get the needed output I am supposed to
get. Can any one suggest me what I am missing? Thank you!

Assignment:
The last exercise in this chapter continues with the exercise from the last chapter, the calculator. In this exercise, expand the existing code by implementing the following new features: (A) Calculator does not automatically quit when the result is given, allowing user to do new calculations. The user has to select “6” in the menu to exit the program. (B) The calculator shows the selected numbers in the main menu by printing “Current numbers:” and the user-given input. By selecting “5” in the calculator menu, the user can change the given numbers.

When implemented correctly, the program prints out following:

Again, implement the program within one large while True-segment, which is terminated with break if the user selects the option “6”.

Example output

Calculator
Give the first number: 100
Give the second number: 25
(1) +
(2) -
(3) *
(4) /
(5)Change numbers
(6)Quit
Current numbers: 100 25
Please select something (1-6): 5
Give the first number: 10
Give the second number: 30
(1) +
(2) -
(3) *
(4) /
(5)Change numbers
(6)Quit
Current numbers: 10 30
Please select something (1-6): 1
The result is: 40
(1) +
(2) -
(3) *
(4) /
(5)Change numbers
(6)Quit
Current numbers: 10 30
Please select something (1-6): 6
Thank you! 

MY code:

print("Calculator")  
    while True:  
        selrction={1,2,3,4,5,6,}  
        value1 = int(input("Give the first number: "))  
        value2 = int(input("Give the second number: "))  
        print("(1) +n(2) -n(3) *n(4) /n(5)Change numbersn(6)Quit")  
        print("Current numbers: ",value1,value2)  
        selection=int(input("Please select something (1-6): "))  


        if selection==1:  
            print("The result is: ",(value1+value2))  
        elif selection==2:  
            print("The result is: ",(value1-value2))  
        elif selection==3:  
            print("The result is: ", (value1*value2))  
        elif selection==4:  
            print("The result is: ",(value1/value2))  
        elif selection==6:  
            print("Thank you!")  
            break  
        elif selection==5:  
            print("Change numbers")  
            continue  
        else:  
            print("Selection was not correct.")  
            selection+=1

my output

Calculator
Give the first number: 100
Give the second number: 25
(1) +
(2) -
(3) *
(4) /
(5)Change numbers
(6)Quit
Current numbers: 100 25
Please select something (1-6): 5
Change numbers
Give the first number: 10
Give the second number: 30
(1) +
(2) -
(3) *
(4) /
(5)Change numbers
(6)Quit
Current numbers: 10 30
Please select something (1-6): 1
The result is: 40
Give the first number: 6
Give the second number:
Asked By: user3358884

||

Answers:

You need to use a flag (e.g. promptForNumbers) to determine whether to prompt for the values again in the loop. Set the flag to True at the very start before the loop, and only execute the value capture of inputs value1 and value2 if the flag is True. Once the numbers are captured, set the flag to False. Then, only set the flag to True again when option 5 is selected (change numbers).

Other points of note:

  • The continue statement is redundant, too, as there’s nothing that follows the if/elsif/elsif/../else statement.
  • selrction={1,2,3,4,5,6,} statement is redundant (and misspelled), as next assignment to selection is another assignment, and there’s no use in-between.
  • selection+=1 is also redundant, as it’s not used.
  • casting input to integer int(input(value)), rather than float float(input(value)), will limit the accuracy of the calculator
  • changed indentation to be consistent with expected Python format
  • there’s no error handling for division-by-zero processing (so if 0 is entered as the second number, then division / selected, it’ll exit with an exception)

So here’s the code with changes as described above:

print("Calculator")

# Flag to indicate whether user should be prompted for input numbers
promptForNumbers = True

while True:

    if promptForNumbers:
        value1 = float(input("Give the first number: "))
        value2 = float(input("Give the second number: "))
        # Set prompt flag false to prevent re-prompting for numbers upon next loop
        promptForNumbers = False

    print("(1) +n(2) -n(3) *n(4) /n(5)Change numbersn(6)Quit")
    print "Current numbers: %s, %s" % (value1, value2)
    selection = int(input("Please select something (1-6): "))

    if selection == 1:
        print("The result is: %s" % (value1 + value2))
    elif selection == 2:
        print("The result is: %s" % (value1-value2))
    elif selection==3:
        print("The result is: %s" % (value1*value2))
    elif selection==4:
        print("The result is: %s" % (value1/value2))
    elif selection==6:
        print("Thank you!")
        break
    elif selection==5:
        # Set prompt flag so that numbers will be requested upon next loop
        promptForNumbers = True
    else:
        print("Selection was not correct.")
Answered By: CJBS
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.