2

I just started on python and since I started a new calculator project, pyCharm spits out none after everything. I'm not sure what's causing this error, I would appreciate it if I could get some help here. (This is just the main function that I'm showing) Here's the code:

def main():
    run = True
    while run == True:
        if run == False:
        break
    try:
        operation = input(print("Would you like to *, -, + or /?"))

        if operation != "+" and operation != "-" and operation != "/" and operation != "*":
            print("invalid input.")
            go = input(print("Would you like to continue, yes or no?"))

        if go == "no":
            run = False

        else:
            continue

        else:
            num1 = int(input(print("What's your first number?")))

            num2 = int(input(print("What's your second number?")))

         if operation == "*":
            print(multi(num1, num2))

         if operation == "-":
            print(sub(num1, num2))

         if operation == "+":
               print(add(num1, num2))

         if operation == "/":
               print(div(num1, num2))

         go = input(print("Would you like to make another calculation, yes or no?"))
         if go == "no":
                run = False
                else:
                    continue

        except:
            print("invalid input.")
            go = input(print("Would you like to continue, yes or no?"))
            if go == "no":
                run = False
            else:
               continue

An example of what happens:

Would you like to *, -, + or /?
None/
What's your first number?
None3
What's your second number?
None4
0.75
Would you like to make another calculation, yes or no?
Noneno

Process finished with exit code 0
4
  • Please fix the indentation in lines 2-4. It is wrong. Also, if run==True in the loop header, it cannot be False on the next line. Commented Jun 24, 2018 at 16:41
  • def multi(num1, num2): return num1 * num2 def add(num1, num2): return num1 + num2 def sub(num1, num2): return num1 - num2 def div(num1, num2): return num1 / num2 Commented Jun 24, 2018 at 16:43
  • 2
    The print functions in your input statements return None, and that's why you see None before the user input. Remove the calls to print(), input will print the text anyway. Commented Jun 24, 2018 at 16:43
  • Does this answer your question? print statement inside of input returns with a "none" Commented Oct 27, 2020 at 11:15

3 Answers 3

4

Remove print statements from input calls:

input(print("What's your first number?")) -> input("What's your first number?\n") or input("Your first number: ")

That None is the return value of print function that is displayed by input function.

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you so much for the help.
1

The function signature for input is input([prompt]). The brackets indicate that a prompt is optional but basically input is expecting you to give a string to print out. Instead, you are giving it a print() statement. The return value of a print statement is None so that's getting outputted as the prompt by the input statement.

Note, it is also bad practice to have an except statement without specifying an error type. Without specifying a type, the except will trigger for any error (for example, it triggered when I tried running your code before adding in your functions like multi). I think you want except ValueError here.

Comments

1

Try removing the print statement in the input.
Instead of input(print("Would you like to continue, yes or no?")) , Try
input("Would you like to continue, yes or no?")
That is what i do

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.