Home > Blockchain >  Python skipping a true if statement
Python skipping a true if statement

Time:11-18

I was trying to build a simple calculator app from scratch in Python as a school project, but the Python environment doesn't seem to accept my if functions.

This is my code:

# Calculator Script
print("At this moment this script is only able to support basic calculating like adding, subtracting, dividing and multiplying a maximum of 2 numbers at a time.")

val1 = input("Enter First Value: ")
val2 = input("Enter Second Value: ")
print("First Value is "  str(val1)  ", Second Value is "  str(val2))

print("Enter Calculation Operation!")
print("1: Addition")
print("2: Subtraction")
print("3: Multiplication")
print("4: Division")
clx = input("Enter your choice: ")

if clx == 1:
    clxprint = str("Adding")
    clx = str(" ")
    print(str(clxprint))
if clx == 2:
    clxprint = str("Subtracting")
    clx = str("-")
if clx == 3:
    clxprint = str("Multiplying")
    clx = str("*")
if clx == 4:
    clxprint = str("Dividing")
    clx = str("/")
else:
    print("Error")
    exit()

print("Processing Operation...")
print(str(clxprint)  " "  str(val1)  "by "  str(val2))

As you can see, it is not complete yet, but I want to get rid of this problem first before I continue. After entering some placeholders for val1 and val2, it doesn't seem to matter which option I enter as it always returns "Error".

Here is the full output:

At this moment this script is only able to support basic calculating like adding, subtracting, dividing and multiplying a maximum of 2 numbers at a time.
Enter First Value: 1
Enter Second Value: 2
First Value is 1, Second Value is 2
Enter Calculation Operation!
1: Addition
2: Subtraction
3: Multiplication
4: Division
Enter your choice: 3
Error

Process finished with exit code 0

CodePudding user response:

Python input() function return a string type. You're using clx as a string type and comparing it to some integer values, so the if statement is never true, and you always go to the 'else' statement.

To fix this, write as follow:

clx = int(input("..."))

CodePudding user response:

Gius's answer is correct. This is just a tip. Use elif instead of if where you can. In your code you have an if to check every possible clx value and while his is fine, you really should be using an elif. An elif is just an else if and doesn't start a new if code block. In your original code, your 4th if block will always run and so your error message might print even though one of the other options was selected. This will not happen with elifs

if clx == 1:
    clxprint = str("Adding")
    clx = str(" ")
    print(str(clxprint))
elif clx == 2:
    clxprint = str("Subtracting")
    clx = str("-")
elif clx == 3:
    clxprint = str("Multiplying")
    clx = str("*")
elif clx == 4:
    clxprint = str("Dividing")
    clx = str("/")
else:
    print("Error")
    exit()

Also in your print statements, you don't have to convert all your variables with str, they are all already strings.

CodePudding user response:

Are you sure you checking an integer and not a string?

  • Related