Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
While loop explanation
#1
num1 = input("Please enter your first number: ")
num2 = input("please enter your second number: ")
op = input("PLease select your operation: ")

while op != "-" or "/" or "+" or "*":

    if op == "*":
        a = float(num1)*float(num2)
        print(a)
        
    elif op == "/" :
        b = float(num1)/float(num2)
        print(b)
        
    elif op == "-":
        c = float(num1)-float(num2)
        print(c)
       
    elif op == "+":
        d = float(num1)+float(num2)
        print(d)
        
    else:
        print("Invalid")
        op = input("Please enter a valid operation:  ")
This is my code. I'm just trying to create a simple calculator. When I input *,/,+,- the program gives me an infinite loop. When i input anything other then *,/,+,- I don't get an infinite loop. Why is that?

Also,

I thought that if i set "op" equal to * or / or + or - the program wouldn't allow "op" to pass through the program, due to the condition "while op != "-" or "/" or "+" or "*":" but "op" clearly passes through.

Am I confused about how while loops work?


Thank you
Reply
#2
It looks like syntax is the misunderstanding here. Your loop condition is:

while op != "-" or "/" or "+" or "*":
which is equivalent to:

while op != "-" or True or True or True:
Of course, that should also give you the infinite loop you had with "*,/,+,-", but it would not raise an error.

If I understand what you want to do here, you should use "in" instead:

while op not in "-/+*":
This will cause the loop to continue so long as the variable op is not one of the listed values. Once it equals one of the values, it will end.

As for the infinite loop issue, the problem is that op is never reset. So, if you enter "+" as op, the properly written loop will:

  1. Evaluate "+", let the loop continue
  2. Perform the associate code for "+"
  3. Repeat step 1

To end the loop, you need a new input for op. The best way to do this is to move the op input inside the loop (I added some extra too):

num1 = input("Please enter your first number: ")
num2 = input("please enter your second number: ")
op = "+"
 
while op not in "-/+*":
    op = input("Please select your operation: ")

    if op == "quit":
        break

    if op not in ("-", "/", "+", "*"):
        print("Invalid entry")
        continue
 
    if op == "*":
        answer = float(num1)*float(num2)
    elif op == "/" :
        answer = float(num1)/float(num2)
    elif op == "-":
        answer = float(num1)-float(num2)
    elif op == "+":
        answer = float(num1)+float(num2)

    print(answer)
There are a lot more improvements to be made, but I'm sure you'll work on this some more.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Explanation of code ejKDE 4 308 Feb-26-2024, 02:50 PM
Last Post: ejKDE
  A better explanation of the last post Led_Zeppelin 9 2,323 Sep-20-2022, 05:08 PM
Last Post: deanhystad
  Operator meaning explanation Sherine 3 1,980 Jul-31-2021, 11:05 AM
Last Post: Sherine
  Explanation of except ... as : Fernando_7obink 2 1,887 Feb-13-2021, 04:45 AM
Last Post: deanhystad
  .maketrans() - a piece of code which needs some explanation InputOutput007 5 2,902 Jan-28-2021, 05:05 PM
Last Post: buran
  .remove() from a list - request for explanation InputOutput007 3 2,178 Jan-28-2021, 04:21 PM
Last Post: InputOutput007
  Explanation of the left side of this statement please rascalsailor 3 2,451 Sep-09-2020, 02:02 PM
Last Post: rascalsailor
  Need explanation of one line of code Fliberty 6 3,410 Feb-18-2020, 12:50 AM
Last Post: Fliberty
  explanation of code hikerguy62 2 2,220 Aug-01-2019, 01:37 PM
Last Post: hikerguy62
  Lamda function explanation mrcool4 4 3,493 Jul-04-2018, 10:44 AM
Last Post: mrcool4

Forum Jump:

User Panel Messages

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