# Python 3 Calculator Error - Help

Page 1 of 1

## 5 Replies - 2288 Views - Last Post: 24 August 2012 - 10:02 PMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'http://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=288394&amp;s=af582d08c400b95453426467ec0ba66f&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 Miikalsen

• D.I.C Head

Reputation: 2
• Posts: 85
• Joined: 29-December 11

# Python 3 Calculator Error - Help

Posted 09 August 2012 - 04:41 AM

Okay, so i have done some hours with python last night and i really really like it. So I'm continuing with it for sure.

But I'm trying to create this calculator using methods. But there seem to be a problem in the code which i can not grasp..

Could you take a look and see if you find any thing wrong ?

I have basically created the main part of the code, where it returns to start if the user chooses after doing a calculation. I added the if case for every option within the choice at the start.

from math import sqrt

```def begin():
print("Welcome to PyCalculus ")
print("1. Addition")
print("2. Substraction")
print("3. Multiplication")
print("4. Division ")
print("5. Square Root")

decide = int(("Choose your type :"))

if decide == 1:
val1 = double(("First value: "))
val2 = double(("Second value: "))
val3 = (val1 + val2)
print ("The total value of " + val1 + val2 +  "is " + val3)
elif decide == 2:
print("Coming")
elif decide == 3:
print("Coming")
elif decide == 4:
print("coming")
elif decide == 5:
print("coming")
else :
print ("Wrong value")
startIver()
begin()

def startIver ():

restartPro = input("Do you want to restart the program ? (Y/N)")

if restartPro == "Y":
begin();

else:
print ("Alright then, see you later.")

```

I hope that you can find what's wrong, cause i can not.. Python is somewhat different from java when it comes to this. Because it would work in java, and show the error if there was one, but it dose not here. Using Netbeans as IDE for both.

Thanks in advance.

Is This A Good Question/Topic? 0

## Replies To: Python 3 Calculator Error - Help

### #2 sepp2k

• D.I.C Lover

Reputation: 2307
• Posts: 3,568
• Joined: 21-June 11

## Re: Python 3 Calculator Error - Help

Posted 09 August 2012 - 04:48 AM

Miikalsen, on 09 August 2012 - 01:41 PM, said:

I hope that you can find what's wrong, cause i can not.. Python is somewhat different from java when it comes to this. Because it would work in java, and show the error if there was one, but it dose not here.

I'm not sure what you mean by that. Running your code most certainly will give you an error message, specifically:

Quote

ValueError: invalid literal for int() with base 10: 'Choose your type :'

Note that running the equivalent Java code (Integer.parseInt("Choose your type:")) will cause a very similar exception to be thrown, so there's not much difference between Python and Java there.

PS: This is unrelated to your current problem, but the call to begin() should be at the bottom of the file. Otherwise the startIver method won't be defined when you call it in begin.

PPS: Using (possibly infinite) recursion to make your program repeat is not a good idea in Python because it can cause a stack overflow when the user starts over too often (the same is true for Java). You should be using a while-loop for that.

This post has been edited by sepp2k: 09 August 2012 - 04:53 AM

Was This Post Helpful? 0

### #3 Miikalsen

• D.I.C Head

Reputation: 2
• Posts: 85
• Joined: 29-December 11

## Re: Python 3 Calculator Error - Help

Posted 09 August 2012 - 04:59 AM

sepp2k, on 09 August 2012 - 04:48 AM, said:

Miikalsen, on 09 August 2012 - 01:41 PM, said:

I hope that you can find what's wrong, cause i can not.. Python is somewhat different from java when it comes to this. Because it would work in java, and show the error if there was one, but it dose not here.

I'm not sure what you mean by that. Running your code most certainly will give you an error message, specifically:

Quote

ValueError: invalid literal for int() with base 10: 'Choose your type :'

Note that running the equivalent Java code (Integer.parseInt("Choose your type:")) will cause a very similar exception to be thrown, so there's not much difference between Python and Java there.

PS: This is unrelated to your current problem, but the call to begin() should be at the bottom of the file. Otherwise the startIver method won't be defined when you call it in begin.

PPS: Using (possibly infinite) recursion to make your program repeat is not a good idea in Python because it can cause a stack overflow when the user starts over too often (the same is true for Java). You should be using a while-loop for that.

alright how would i do a while loop to return the program if the user chooses too ?
¨

And. I had it at the end at first the begin method, but i still get a wierd error:

```Traceback (most recent call last):
Welcome to PyCalculus
File //Location Removed line 45, in <module>
1. Addition
2. Substraction
3. Multiplication
4. Division
5. Square Root
begin()
File //location removed line 14, in begin
decide = int(("Choose your type :"))
ValueError: invalid literal for int() with base 10: 'Choose your type :'
```

This post has been edited by Miikalsen: 09 August 2012 - 05:00 AM

Was This Post Helpful? 0

### #4 sepp2k

• D.I.C Lover

Reputation: 2307
• Posts: 3,568
• Joined: 21-June 11

## Re: Python 3 Calculator Error - Help

Posted 09 August 2012 - 05:16 AM

Miikalsen, on 09 August 2012 - 01:59 PM, said:

alright how would i do a while loop to return the program if the user chooses too ?
¨

Instead of handling invalid input as part of your if-block, add a while-loop before the if-block that loops until the input entered by the user is a valid integer between 1 and 5.

Quote

And. I had it at the end at first the begin method, but i still get a wierd error:
[...]
ValueError: invalid literal for int() with base 10: 'Choose your type :'[/code]

As I indicated in my first post, what you're doing on that line is equivalent to doing Integer.parseInt("Choose your type:") and it throws an exception for the same reason as that Java code would throw an exception: "Choose your type:" is not a valid integer.

I'm guessing you might have intended to call the input there.
Was This Post Helpful? 0

### #5 den510

• New D.I.C Head

Reputation: 0
• Posts: 6
• Joined: 22-August 12

## Re: Python 3 Calculator Error - Help

Posted 24 August 2012 - 02:42 PM

A few pointers:

1. Yes, using a while loop instead of recursion is the way to go here. Stack overflows are to be avoided at all costs.

2. You may want to consider using "raw_input" instead of "input". This is more secure and stable, and is the preferred method for accepting user input.

You might try something like:

```from os import system

is_valid = "False"
while(is_valid == "False"):
###Have all your menu options here:
###Call from system('clear') or in windows system('cls')
choice = raw_input("Enter your choice: ")
if choice != "1" and choice != "2" and choice != "3" and choice != "4" and choice != "5":
print("invalid choice!")
else:
is_valid = "true"

```

Formatting is a plus in text based programs, be sure to start familiarizing yourself with python's rich array of formatting tools, the most basic of which is to be able to clear the screen.

Also, whenever you're accepting input, it will almost always be in string format in python. Instead of type casting it to int, you may just consider skipping that step in option checking... It will save you time and headaches.

Good luck to you sir!

-Dennis

Sorry, put the clear statement in the if statement, before invalid choice. My bad.
Was This Post Helpful? 0

### #6 sepp2k

• D.I.C Lover

Reputation: 2307
• Posts: 3,568
• Joined: 21-June 11

## Re: Python 3 Calculator Error - Help

Posted 24 August 2012 - 10:02 PM

den510, on 24 August 2012 - 11:42 PM, said:

2. You may want to consider using "raw_input" instead of "input". This is more secure and stable, and is the preferred method for accepting user input.

Given that he put parentheses around the arguments to print, I'm pretty sure he's using Python 3. So that point does not apply.

Quote

Formatting is a plus in text based programs, be sure to start familiarizing yourself with python's rich array of formatting tools, the most basic of which is to be able to clear the screen.

Please don't. Not only is clearing the screen annoying in most cases (note that most real console applications don't clear the screen unless they're drawing windows using curses), it makes your code platform specific (or force you to check the platform in an if statement).

PS: Using strings as booleans is just horrible. Please don't ever do that.
Was This Post Helpful? 1

Page 1 of 1

 .related ul{list-style-type:circle;font-size:12px;font-weight:bold;}.related li{margin-bottom:5px;background-position:left 7px!important;margin-left:-35px;}.related h2{font-size:18px;font-weight:bold;}.related a{color:blue;}