Posts: 9
Threads: 3
Joined: Jun 2021
Jun-06-2021, 12:05 AM
(This post was last modified: Jun-06-2021, 01:32 AM by Larz60+.)
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")
Posts: 12,025
Threads: 484
Joined: Sep 2016
Jun-06-2021, 01:35 AM
(This post was last modified: Jun-06-2021, 01:35 AM by Larz60+.)
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
Posts: 9
Threads: 3
Joined: Jun 2021
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")
Posts: 2,168
Threads: 35
Joined: Sep 2016
Jun-06-2021, 06:09 PM
(This post was last modified: Jun-06-2021, 06:14 PM by Yoriz.)
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]")
Posts: 9
Threads: 3
Joined: Jun 2021
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]")
Posts: 6,779
Threads: 20
Joined: Feb 2020
Can you use a function with a return?
Posts: 9
Threads: 3
Joined: Jun 2021
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?
Posts: 6,779
Threads: 20
Joined: Feb 2020
Jun-07-2021, 04:16 PM
(This post was last modified: Jun-07-2021, 04:16 PM by deanhystad.)
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 ')
Posts: 9
Threads: 3
Joined: Jun 2021
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)
Posts: 6,779
Threads: 20
Joined: Feb 2020
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}')
|