Python Forum
Python calculator help but not using while loop with true, any flags variable ,break
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Python calculator help but not using while loop with true, any flags variable ,break
#1
Hi there ,
I am new to python and having hard time coding basic calculator but the catch is that we cannot use while loop with break statement or while true or any flags variable to set condition before while loop . Now the thing is that that when user inputs num2=0 , it should display the results are wrong and reprompt for another divisor , and loop it until user enters divisor that is not 0 and also If the selected operation is a division & the divisor is zero re-prompt for a number until a
valid divisor has been entered by the user.
The user can use the calculator until s/he decides to quit.
please help i been trying this code for many hours its just skipping after selecting / and goes to input num3 but after that it just doesnt loops using 2nd while loop.

num1=float(input(“enter a number “)
num2=float(input(“enter a number “)

Operand=input(“please select operator -,/,+,* or [q to quit]”)

While operand !=q :
   If operand == ‘+’:
      Print (“the results are :” , num1 + num2)

    Elif operand == '-' :
       Print ("the results are :" , num1 - num2)

    elif operand == '*' :
       print ("the results are :" , num1 * num2 )

    elif operand == '/':
       if num1 == '0':
          print ("the results are wrong")
       else :
          num3=float(input("enter a number :"))
          while num3 == '0':
             num4=float(input("please enter a number :"))
             if num4 != "0"
                 print ("the results are ", num1 / num3)
    operand = input ("please select operator +,-,/,* or [q to quit] :")

      print ("thank you for using the program")
Reply
#2
line 10, python is case sensitive, use elif not Elif
line 11, use print, not Print
There are indentation errors as well

It's best to write a samll amount of code, test, correct any errors

repeat this process until complete.

As you gain experience, you can write more code before testing, but enen then, test often
Reply
#3
Hi thank you Larza for your kind reply , i did some troubleshooting and made my 2nd while loop work but the problem is that while loop exits but when I give it q option it still prints result , it not printing the end line can you help me with that this is due tonight ? been working for long hours on this .
Any help is appreciated
Output:
num1=float(input('please enter a number'))
num2=float(input('please enter a number'))

operand=input("please chose operator /,+,*,/ or [q to quit]")

while operand != 'q':
    if operand == '+':
        sum = 0
        sum+= num1 + num2
        print ("the results are " sum)
    elif operand == '-':
        print ("the results are ", num1 - num2 )
    elif operand == '*':
        print ("the results are "[output][output][output][output][output][output][output][output][output][output][output][output][output][output][output][output][output][/output][/output][/output][/output][/output][/output][/output][/output][/output][/output][/output][/output][/output][/output][/output][/output][/output], num1 * num2 
    elif operand == '/':
        if num2 == 0:
            print ("division cannot be done as divisor is zero")
        num3=float(input("please enter non zero number"))
        while num3 != 0:
            print (" the results are ", num1 / num3 )
            operand=input("please chose operator /,+,*,/ or [q to quit]")
    print ("thank you")
Reply
#4
On line 8 sum has been assigned to 0, sum is a built-in function, be careful not to overwrite built-in functions as they wont do what they where intended to do anymore.
sum = 0

There is a syntax error on line 10
print ("the results are " sum) # syntax error on this line

On line 18 a non zero number is asked for even when num2 was a non zero number
num3=float(input("please enter non zero number"))

The value of num3 on line 19 is never changed inside of the loop so this will loop forever
while num3 != 0:

The input on line 21 is only accesable when elif operand == '/': is true
operand=input("please chose operator /,+,*,/ or [q to quit]")
Reply
#5
in line 18 new number has been asked as if num2 which is the divisor is zero (condition of if on line 16), how do i break the loop becuase i thought asking user about operand choice would break it but it keeps printing the result

(Jun-06-2021, 06:09 PM)Yoriz Wrote: On line 8 sum has been assigned to 0, sum is a built-in function, be careful not to overwrite built-in functions as they wont do what they where intended to do anymore.
sum = 0

There is a syntax error on line 10
print ("the results are " sum) # syntax error on this line

On line 18 a non zero number is asked for even when num2 was a non zero number
num3=float(input("please enter non zero number"))

The value of num3 on line 19 is never changed inside of the loop so this will loop forever
while num3 != 0:

The input on line 21 is only accesable when elif operand == '/': is true
operand=input("please chose operator /,+,*,/ or [q to quit]")
Reply
#6
Can you use a function with a return?
Reply
#7
no instrutor said only to use while loop but without assigning flag variable or break statement
(Jun-07-2021, 02:31 AM)deanhystad Wrote: Can you use a function with a return?
Reply
#8
Your program doesn't allow solving multiple equations, one of your stated requirements. You ask for the operator inside a loop but you don't ask for the operands. Given that you want to allow solving multiple equations and want to quit when the user enters "q", you loop must look something like this:
while op != 'q':
    op = input("please chose operator +, -, *, / or q to quit ")
    if op in ('+', '-', '*', '/'):  # Does some minimal input checking
        num1 = float(input('enter a number '))
        num2 = float(input('enter a number '))
Because quitting is an option for the operator it doesn't make sense asking for the operands before the operator.

The above code will not compile because op is not defined. You could add a line to get the initial operator, but I really hate duplicate code, even if it is only a single line. I think the correct initial value for op is None.
op = None
while op != 'q':
    op = input("please chose operator +, -, *, / or q to quit ")
    if op != 'q':
        num1 = float(input('enter a number '))
        num2 = float(input('enter a number '))
Try running this and see if it meets your requirements. Can you enter multiple equations? Does it quietly exit if 'q' is entered for the operator? Now all you need to do is add the math.

Oh, about the zero divisor problem. From your description you want the program to loop until the user enters a non-zero divisor. This leads to a very simple and very literal translation from requirements to code. If num2 is the divisor, the way to force the user to enter a non-zero divisor is:
while num2 == 0:
    num2 = float(input('enter a non-zero divisor ')
Reply
#9
no that code not work requirement is if num2 = 0 (divisor is zero), calculator should prompt user again for num2 until it is non zero and displays correct output (num1 /num2) and then exit the loop with op = (please choose ooperator or q to quit)
Reply
#10
It does work for a zero divisor. Did you try it? It will keep looping until you type something that is not zero.

Granted everything crashes horribly if you enter something that cannot be evaluated as a floating point number, but that is probably a programming challenge from your near future.

op = None
while op != 'q':
    op = input("please chose operator +, -, *, / or q to quit ")
    if op != 'q':
        num1 = float(input('enter a number '))
        num2 = float(input('enter a number '))

    if op == '\':
        while num2 == 0:
            num2 = float(input('enter a non-zero divisor ')  # Loop until not zero is entered.
       print(f'{num1} / {num2} = {num1/num2}')
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Calculator Loop Help dock1926 3 3,858 Jun-23-2021, 10:05 PM
Last Post: HereweareSwole
  Weight loss calculator loop error drogers10940 7 14,518 Feb-14-2021, 12:36 AM
Last Post: Larz60+
  while loop on a calculator missus_brown 3 15,073 Feb-10-2019, 08:19 PM
Last Post: ichabod801
  i need help in fixing my scientific calculator coding : (, im using python 3.5.5 hans77 1 4,153 Oct-17-2018, 03:26 AM
Last Post: stullis
  python age calculator need to find the number of years before they turn 100 not using orangevalley 4 9,930 Mar-26-2018, 04:44 AM
Last Post: PyMan
  while true loop candylicious 3 4,180 Nov-06-2017, 06:36 PM
Last Post: gruntfutuk

Forum Jump:

User Panel Messages

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