Find the answer to your Linux question:
Results 1 to 10 of 10
Hello I am trying to make a simle calculator in python, I have everything working except this small bit of code that won't seem to work right. It seems to ...
Enjoy an ad free experience by logging in. Not a member yet? Register.
  1. #1
    Linux Newbie
    Join Date
    Sep 2012
    Posts
    150

    Stuck in python loop


    Hello I am trying to make a simle calculator in python, I have everything working except this small bit of code that won't seem to work right. It seems to get stuck in a while loop that I have going. I have tried everyting and don't know what else to do. The code is below:

    Code:
    import os
    
    loop = True
    while loop:
    	os.system("clear")
    	firstNumber = raw_input("Enter the first number: ")
    	firstOperator = raw_input("Enter the operator: ")
    	secondNumber = raw_input("Enter the second number ")
    	pi = 3.14159
    	
    	loop2 = True
    	while loop2:
    		
    		if not firstOperator == "+" or "-" or "*" or "/" or "<" or ">":
    			print "Invalid Operator"
    		
    		else:
    			loop2 = False
    			
    		
    		
    		if firstOperator == "+":
    			print int(firstNumber) + int(secondNumber)
    	
    		if firstOperator == "-":
    			print int(firstNumber) - int(secondNumber)
    	
    		if firstOperator == "*":
    			print int(firstNumber) * int(secondNumber)
    	
    		if firstOperator == "/":
    			print float(firstNumber) / float(secondNumber)
    	
    		if firstOperator == ">":
    			print bool(firstNumber) > bool(secondNumber)
    	
    		if firstOperator == "<":
    			print bool(firstNumber) < bool(secondNumber)
    		
    		decision = raw_input("Would you like to do another calculation? y,n: ")
    	
    		if decision == "y":
    			loop = True
    	
    		if decision == "n":
    			loop = False

  2. #2
    Linux Guru Rubberman's Avatar
    Join Date
    Apr 2009
    Location
    I can be found either 40 miles west of Chicago, in Chicago, or in a galaxy far, far away.
    Posts
    11,963
    I think you need to indent the lines in your 'else:' block after 'loop2 = False' again. I believe that without the additional indentation, they will be evaluated after the if/else statement and conditional operation.
    Sometimes, real fast is almost as good as real time.
    Just remember, Semper Gumbi - always be flexible!

  3. #3
    Penguin of trust elija's Avatar
    Join Date
    Jul 2004
    Location
    Either at home or at work or down the pub
    Posts
    3,674
    Your logic is flawed. It looks like loop2 is a validation loop but you never give the user the chance to correct their input. Have a look at this version which does what I think you were aiming for

    PHP Code:
    import os

    # Finished is a better variable name than loop as it is descriptive of the intent
    finished False
    while not finished:
        
    os.system("clear")
        
    pi 3.14159
        
        
    # This is where the program gets and validates the input. It is in it's own loop that is exited when the validation is passed.
        
    valid False
        
    while not valid:
        
            
    firstNumber raw_input("Enter the first number: ")
            
    firstOperator raw_input("Enter the operator: ")
            
    secondNumber raw_input("Enter the second number ")
            
            
    #  Note the or firstOperator == for every condition you are checking
            
    if firstOperator == "+" or firstOperator == "-" or firstOperator == "*" or firstOperator == "/" or firstOperator == "<" or firstOperator == ">":
                
    valid True
            
        
    if firstOperator == "+":
            print 
    int(firstNumber) + int(secondNumber)

        if 
    firstOperator == "-":
            print 
    int(firstNumber) - int(secondNumber)

        if 
    firstOperator == "*":
            print 
    int(firstNumber) * int(secondNumber)

        if 
    firstOperator == "/":
            print 
    float(firstNumber) / float(secondNumber)

        
    #  Not sure why these were converted to bool?
        
    if firstOperator == ">":
            print 
    firstNumber secondNumber

        
    if firstOperator == "<":
            print 
    firstNumber secondNumber

        decision 
    raw_input("Would you like to do another calculation? y,n: ")

        if 
    decision == "n":
            
    finished True 
    "I used to be with it, then they changed what it was.
    Now what was it isn't it, and what is it is weird and scary to me.
    It'll happen to you too."

    Grandpa Simpson



    The Fifth Continent

  4. $spacer_open
    $spacer_close
  5. #4
    Linux Newbie
    Join Date
    Sep 2012
    Posts
    150
    Oh wow you simplified that well. How long have you been doing python? Thanks. I converted the firstNumber > secondNumber to bool because they are saying true or false isn't that a boolean statement I thought that that was necessary.

  6. #5
    Linux Newbie
    Join Date
    Sep 2012
    Posts
    150
    OH so the validation loop is assumed that the input in invalid until it is varified as valid. I would have never thought of that. really smart.

  7. #6
    Linux Guru Rubberman's Avatar
    Join Date
    Apr 2009
    Location
    I can be found either 40 miles west of Chicago, in Chicago, or in a galaxy far, far away.
    Posts
    11,963
    That is where having someone with a real knowledge of Python, such as Elija, is good. Myself, I have a rudimentary understanding of it.
    Sometimes, real fast is almost as good as real time.
    Just remember, Semper Gumbi - always be flexible!

  8. #7
    Linux Newbie
    Join Date
    Sep 2012
    Posts
    150
    yeah that is true thanks for the reply though

  9. #8
    Penguin of trust elija's Avatar
    Join Date
    Jul 2004
    Location
    Either at home or at work or down the pub
    Posts
    3,674
    Quote Originally Posted by awesomepilot View Post
    Oh wow you simplified that well. How long have you been doing python? Thanks. I converted the firstNumber > secondNumber to bool because they are saying true or false isn't that a boolean statement I thought that that was necessary.
    I've been exploring Python for a couple of months now but programming in various languages for over 30 years.

    The result of a comparison will always be true or false but what you are comparing doesn't have to be. And on that note, are you sure you need int()? What happens if you try and add 1.5 and 2.3? Is it right? The float on the division is a bit more interesting. The traditional way is to use
    Code:
    num1 div float(num2)
    which will still return a float with one less operation but in the case you need the answer to be accurate it can introduce subtle math errors. The recommended approach is to import the Python 3 division and use that instead as it is far more accurate.
    Code:
    from __future__ import division
    a = 10
    b = 3
    c = a / b
    Quote Originally Posted by awesomepilot View Post
    OH so the validation loop is assumed that the input in invalid until it is varified as valid. I would have never thought of that. really smart.
    One thing you learn very quickly when other people use your code is to always assume invalid input. What happens in this program when the user enters
    Code:
    one
    +
    7
    The validation loop probably needs sorting out for that kind of input.

    Quote Originally Posted by Rubberman View Post
    That is where having someone with a real knowledge of Python, such as Elija, is good. Myself, I have a rudimentary understanding of it.
    Not yet but I'm working on it It's why I always advocate learning to program rather than learning a language although the two can be concurrent. Once you understand the concepts, you can carry those into other languages and then it's just a matter of getting your head around the syntax. Python seems to be an excellent language to learn to program in as they have kept the syntax fairly simple. At the risk of sounding like an old fart it feels more like HiSoft Basic on the Atari ST than any other language I've coded in since then!
    Last edited by elija; 08-11-2013 at 12:21 PM. Reason: Accidentally submitted instead of previewing
    "I used to be with it, then they changed what it was.
    Now what was it isn't it, and what is it is weird and scary to me.
    It'll happen to you too."

    Grandpa Simpson



    The Fifth Continent

  10. #9
    Linux Newbie
    Join Date
    Sep 2012
    Posts
    150
    ok when I put 3 + 3 it gives me 33 as the output. But when I put int(3) + int(3) then it gives me 6. I did know about the float for the decimals but I also see about the validation loop being its own loop. I didn't know that you could just indent the loop I thought that you had to indent the whole program. That really helps too.

  11. #10
    Penguin of trust elija's Avatar
    Join Date
    Jul 2004
    Location
    Either at home or at work or down the pub
    Posts
    3,674
    Quote Originally Posted by awesomepilot View Post
    ok when I put 3 + 3 it gives me 33 as the output. But when I put int(3) + int(3) then it gives me 6.
    That's interesting, I thought that the duck typing used by Python would have seen them as numbers but it appears that raw_input() always returns a string so it isn't automatically translated. Which I suppose makes sense now that I think about it.
    "I used to be with it, then they changed what it was.
    Now what was it isn't it, and what is it is weird and scary to me.
    It'll happen to you too."

    Grandpa Simpson



    The Fifth Continent

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •