•

Check out our Community Blogs

Register and join over 40,000 other developers!

### Recent Blog Entries

• phi

I love this community !

• JackJames

hi i am jack i am seo expert jack james would love you to read new post

# Help with Python calculator

calculator

3 replies to this topic

### #1 TheTyga

TheTyga

CC Regular

• Member
• 29 posts
• Location:WA, spanaway
• Programming Language:Python
• Learning:C, C++, Objective-C, C#, PHP

Posted 30 June 2012 - 03:58 PM

So, I am completely new to python, well coding in genral, and I wanted to build a calculator just for a little beginers task, and I came across some flaws, here is my code : [NOTE: it's not complete]

```dash = '*'
print dash * 80
print dash * 70
print dash * 60
print dash * 50
print("Mason\'s Calculator!")
print dash * 40
print dash * 30
print dash * 20
print dash * 10
print("Choose an equation!")
print dash * 20
print(" A - (-), B - (+), C - (/), D - (*)")
equation_picked = raw_input()
if equation_picked == 'a' or equation_picked == 'A':
first_value = input("Enter the first value : ")
second_value = input("Enter the second value : ")
print ("Your result is : ")
print first_value, '-',  second_value, '=', first_value - second_value
print dash * 30
print dash * 40
print dash * 50
print dash * 60
print dash * 70
print dash * 80

else:
equation_picked == 'b' or equation_picked == 'B'
add_value_1 = input("Enter First value : ")
add_value_2 = input("Enter Second value : ")
print ("Your result is : ")
print dash * 70
print dash * 60
print dash * 50
print dash * 40
print dash * 30
print dash * 20
print dash * 10
if equation_picked == 'c' or equation_picked == 'C':
sub_value_1 = input("Enter First value : ")
sub_value_2 = input("Enter second value : ")
```

# Whenever I go and run the code, say for instance I want to divide, so I run the code and select the letter 'C' well when I put in the values say 12 & 6, it adds thim? Example :

********************************************************************************
**********************************************************************
************************************************************
**************************************************
Mason's Calculator!
****************************************
******************************
********************
**********
```Choose an equation!
********************
A - (-), B - (+), C - (/), D - (*)
c
Enter First value : 12
Enter Second value : 1
12 + 1 = 13
**********************************************************************
************************************************************
**************************************************
****************************************
******************************
********************
**********
Enter First value : 12
Enter second value : 1```

#then it goes on with the rest of the code? I'm confused and i'm sure my code has alot of flaws, like I said I began coding for the first time about 4 hours ago.
• 0

### #2 Alexander

Alexander

YOL9

• Moderator
• 3963 posts
• Location:Vancouver, Eh! Cleverness: 200
• Programming Language:C, C++, PHP, Assembly

Posted 30 June 2012 - 11:51 PM

Welcome Mase,

Your issue remains in your ELSE statement. It is meant to capture everything that was not captured in the previous IF statements.

The program flow would be the following..

```Input: C
Accepted 'C':
Is C 'a' or 'A'?
Yes? Prompt and Subtract.
No(else)? Continue
Is C 'b' or 'B'? (do nothing)
End

is C 'c' or 'C'?
Prompt for first value
Prompt for second value
End
```

Can you apply my simple logical example to your actual code and understand what is happening?

Your else: (and IF for 'b') is not formed correctly.

You may wish to use IF/ELSEIF/ELSEIF/.../ELSE statements rather than just if + else.

The final ELSE is optional, but can handle the case where the user enters something incorrect such as 'L', or, you can quit the program if they enter something incorrect as a feature - You decide!

Alexander.
• 0

All new problems require investigation, and so if errors are problems, try to learn as much as you can and report back.

### #3 tpPacino

tpPacino

CC Regular

• Member
• 29 posts
• Location:Sarajevo, Bosnia and Herzegovina
• Programming Language:C, Java, C++, PHP, (Visual) Basic, PL/SQL, Pascal, Others
• Learning:Objective-C, C#, JavaScript, Ruby, Haskell, Others

Posted 30 June 2012 - 11:56 PM

First of all hi,
your code looks a lot what it should look like, the only thing you are missing is
```if equation_picked == 'a' or equation_picked == 'A':
// code```
```else:
//code```
You see, you need to make an if-elif statement in order for this to work.
When you entered 'c' in the input, the program called the 'else' block of commands, because you didn't cover the 'd' case, only the 'a', so if you input anything other than 'a', you would get the '+' command called.
You would need to do it this way:

```if equation_picker == 'a' or equation_picker == 'A'
#code for the case A
elif equation_picker == 'b' or equation_picker == 'B'
# code for the case B
elif equation_picker == 'c' or equation_picker == 'C'
# code for the case C
elif equation_picker == 'd' or equation_picker == 'D'
#code for the case D
else
# code for the an unknown case
```

Also, I would recommend you to read about switch statements, it's another way of covering different cases.
EDIT: Alexander was faster
• 0

### #4 TheTyga

TheTyga

CC Regular

• Member
• 29 posts
• Location:WA, spanaway
• Programming Language:Python
• Learning:C, C++, Objective-C, C#, PHP

Posted 01 July 2012 - 01:02 PM

I appreciate it you guys, i'm getting the hang of python, it's pretty simple, i'd like to learn PHP afterwards tho, wich I know in terms is alot more time consuming and stressful, but thank you for the responses!

First of all hi,
your code looks a lot what it should look like, the only thing you are missing is

```if equation_picked == 'a' or equation_picked == 'A':
// code```
```else:
//code```
You see, you need to make an if-elif statement in order for this to work.
When you entered 'c' in the input, the program called the 'else' block of commands, because you didn't cover the 'd' case, only the 'a', so if you input anything other than 'a', you would get the '+' command called.
You would need to do it this way:

```if equation_picker == 'a' or equation_picker == 'A'
#code for the case A
elif equation_picker == 'b' or equation_picker == 'B'
# code for the case B
elif equation_picker == 'c' or equation_picker == 'C'
# code for the case C
elif equation_picker == 'd' or equation_picker == 'D'
#code for the case D
else
# code for the an unknown case
```

Also, I would recommend you to read about switch statements, it's another way of covering different cases.
EDIT: Alexander was faster

Welcome Mase,

Your issue remains in your ELSE statement. It is meant to capture everything that was not captured in the previous IF statements.

The program flow would be the following..

```Input: C
Accepted 'C':
Is C 'a' or 'A'?
Yes? Prompt and Subtract.
No(else)? Continue
Is C 'b' or 'B'? (do nothing)
End

is C 'c' or 'C'?
Prompt for first value
Prompt for second value
End
```

Can you apply my simple logical example to your actual code and understand what is happening?

Your else: (and IF for 'b') is not formed correctly.

You may wish to use IF/ELSEIF/ELSEIF/.../ELSE statements rather than just if + else.

The final ELSE is optional, but can handle the case where the user enters something incorrect such as 'L', or, you can quit the program if they enter something incorrect as a feature - You decide!

Alexander.

• 0

### Also tagged with one or more of these keywords: calculator

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download