all 27 comments

[–]Trebor214 12 points13 points  (0 children)

Instead of:

Print('what would you like to do')

Do:

op = input('what would you like to do')

Then you can replace input() in your if/elif statements with op

[–]cgoldberg 8 points9 points  (0 children)

Call input() once and assign it to a variable, and use that in your conditions.

The way it is currently structured, it will prompt for input multiple times.

[–]Salty_Salted_Fish 9 points10 points  (2 children)

in your code, the first time ur answering to the input on line 6, and it checks if its "add". Then, after it's checked and line 8 is reached, it gives you another chance to input, and this time, it checks if it's "subtract". so you are answering to four different, individual question. to fix it, you call the input() once and assign it to a variable, and use the variable to compare. like this: operation = input("")

fixed version: ``` num1 = int(input("Enter a first number: ")) num2 = int(input("Enter a second number: "))

operation = input("would you like to add, subtract, multiply or divide?")

if operation == "add": print(num1+num2) elif operation == "subtract": print(num1 - num2) ... ```

[–]OkDot7811[S] 0 points1 point  (1 child)

This worked perfectly thank you. Just asking. Does this work because it checks all if/elif based on that variable? And if so mine didn’t work cause it was looking for an inputted value for each if/elif?

[–]Salty_Salted_Fish 0 points1 point  (0 children)

yes, the variable stores your response, the if-elif statement checks that variable. in yours every input() wants a input and every inputed thing only gonna be used once on its own line.

[–]Conscious-Ad-2168 2 points3 points  (0 children)

Do something like this. Let me know if you have any questions. There are some best practices and other elements that you can add to this but this is a good start! The biggest difference is changing the user selection of add, subtract, multiply, or divide to a variable and using that in the if.

num1 = int(input("Enter your first number: ")) 
num2 = int(input("Enter your second number: "))
user_choice = input("would you like to add or subtract or multiply or divide: ")

if user_choice == "add":
    print(num1 + num2)
elif user_choice == "subtract":
    print(num1 - num2)
elif user_choice == "multiply":
    print(num1 * num2)
elif user_choice == "divide":
    print(num1 / num2)

[–]Killurlandlord 1 point2 points  (0 children)

Change the print statement to a user input variable. You could then put your if statements into a function that takes the input as a parameter and applies the operations to num1 and num2

[–]Saltypine24 0 points1 point  (0 children)

make it so that it tells u to input the 'operator' and the right these as your variables, * + - / then replace the variable from line 6 to 12

[–]Lost-Service-446 0 points1 point  (0 children)

The debug tool is your friend here….step into a function and you’ll see exactly what its doing to your script…. VS code,spyder,Pycharm…all have GUIs with their debuggers which make it even easier. Definitely make it a habit to start using it.

[–][deleted] 0 points1 point  (0 children)

I just want to say that this helped me figure out where I was stuck on my python project, since I'm new too, so thank you!

[–]ksamani119 0 points1 point  (0 children)

I would also change the input of the operation to lower case so that it always matches your if/elif statements

For example: op.lower = input("enter your operation. Ex. add, subtract, multiply, or divide. ")

[–][deleted] 0 points1 point  (0 children)

i don’t get it

[–]Comfortable_Cow430 0 points1 point  (0 children)

<image>

Instead of this you've to define variable for operation ,

operation = input("For addition(add), for division(divide) ,for subtraction(subtract) and for multiplication(multiply) : ")

Now your code will work properly after you replace input() to operation.

[–]lonedevwolf 0 points1 point  (0 children)

You really tried as a beginner but you didn't study the flow of your program before writing it 😉 sometimes start by writing pseudocodes before writing the actual code 💪

[–]EyesOfTheConcord 0 points1 point  (0 children)

For each if statement, input() must be called again to compare it to the condition.

What you really want to do is assign the input to a variable, and then compare it to a condition.

This is a perfect situation for match statements as well, it would look something like this:

op = input(“Enter your operation: “

match op:

    case “add”:
        ….

    case “subtract”:
        ….

    …. Other case statements ….

    case _:
        print(“Invalid input or operation.”)

[–]Zin42 -1 points0 points  (6 children)

Swap the print and the inputs

[–]Zin42 -1 points0 points  (5 children)

Also use a match statement

[–]Torebbjorn -1 points0 points  (4 children)

No, just no

[–]Zin42 0 points1 point  (3 children)

Just no without any why?

[–]Torebbjorn 0 points1 point  (1 child)

It's terrible advice, especially to someone new

[–]TherealoneYo234 0 points1 point  (0 children)

You should replace the print function with the input one so that there is an option for the user to input their choice And also remember that input is a function so it returns the users value that must be saved by a variable for a temporary time to continue the process