# Calculator

Hey all, i have run into another problem, my code seems to work only, it does not print out the second function, i’ll add in the code below, please help me so i can learn from this mistake

``````print "Hello and welcome to my program!"

print "I have scripted a simple calculator"

enter = raw_input("Type 'enter' to continue! ")

def text():
if enter == 'enter':
print numbers()

def numbers():
print "please give in your numbers!"
number1 = raw_input(int())
number2 = raw_input(int())
print which()

def which(x):
print "which of the ones would you like to use?"
raw_input("*/+/-/ or /?")
if x == "*":
print number1 * number2
elif x == "+":
print number1 + number2
elif x == "-":
print number1 - number2
elif x == "/":
print number1 / number2
else:
print "You did not give in an option!"
numbers()
``````

Everything works, but when i type in ‘enter’ to continue it does not print out numbers()

There are so many problems with this code, the function text never gets called, `print numbers()` will only print something if the function returns something. (which is it doesn’t), you want to take input and cast it to a int: `number1 = int(raw_input())`, i don’t know what you did, but that aint working, you didn’t keep scope in mind, your which function doesn’t know what number1 and number2 are, i would pass them as arguments, i don’t know why you pass x as argument.

Not sure if i covered everything, but here is an updated version:

``````def text():
enter = raw_input("Type 'enter' to continue! ")
if enter == 'enter':
numbers()
else:
print "please type enter to continue"
text()

def numbers():
print "please give in your numbers!"
number1 = int(raw_input())
number2 = int(raw_input())
which(number1,number2)

def which(number1,number2):
print "which of the ones would you like to use?"
x = raw_input("*/+/-/ or /?")
if x == "*":
print number1 * number2
elif x == "+":
print number1 + number2
elif x == "-":
print number1 - number2
elif x == "/":
print number1 / number2
else:
print "You did not give in an option!"
numbers()

print "Hello and welcome to my program!"

print "I have scripted a simple calculator"

text()
``````
1 Like

Hey, thanks for the reply, i’ve used the code in IDLE but it shows up that there is a missing paramether for ‘print’

the code i posted above worked fine on codecademy

as off python 3, print changed, codecademy uses python 2.7, so then print is:

``````print "i am printed"
``````

in python 3 you need parentheses on a print call:

``````print("i am printed")
``````

and raw_input has been replaced with input, so the code (tested for python 3.5.1) is:

``````def text():
enter = input("Type 'enter' to continue! ")
if enter == 'enter':
numbers()
else:
print("please type enter to continue")
text()

def numbers():
print("please give in your numbers!")
number1 = int(input())
number2 = int(input())
which(number1,number2)

def which(number1,number2):
print("which of the ones would you like to use?")
x = input("*/+/-/ or /?")
if x == "*":
print(number1 * number2)
elif x == "+":
print(number1 + number2)
elif x == "-":
print(number1 - number2)
elif x == "/":
print(number1 / number2)
else:
print("You did not give in an option!")
numbers()

print("Hello and welcome to my program!")

print("I have scripted a simple calculator")

text()
``````

Hello again, i have decided to scrap the old code and make a new one, everything seems fine except that all the value’s print out “0”

Here’s my code,

``````print("Welcome to my calculator!")

options = ["*,/,+,-,"]
number1 = int()
number2 = int()
def first_number():
number1 = int(input("Please enter the first number "))

def second_number():
number2 = int(input("Please enter the second number "))

def which(first_number, second_number):
print (options)
typeofsum = str(input("Which of the following would you like to add?"))
if typeofsum == ("*"):
print (number1 * number2)
elif typeofsum == ("/"):
print (number1 / number2)
elif typeofsum == ("+"):
print (number1 + number2)
elif typeofsum == ("-"):
print (number1 - number2)
else:
print ("No option assigned")

first_number()
second_number()
which(number1, number2)
``````

Welcome to my calculator!
Please enter the first number 10
Please enter the second number 10
[’,/,+,-,’]
Which of the following would you like to add?

0

#This is what it prints

i am not surprised it doesn’t work, take a look at this code:

``````print("Welcome to my calculator!")

options = ["*,/,+,-,"]
number1 = int()
number2 = int()
def first_number():
number1 = int(input("Please enter the first number "))

def second_number():
number2 = int(input("Please enter the second number "))

def which(first_number, second_number):
print(number1,number2)
print (options)
typeofsum = str(input("Which of the following would you like to add?"))
if typeofsum == ("*"):
print (number1 * number2)
elif typeofsum == ("/"):
print (number1 / number2)
elif typeofsum == ("+"):
print (number1 + number2)
elif typeofsum == ("-"):
print (number1 - number2)
else:
print ("No option assigned")

first_number()
second_number()
which(number1, number2)
``````

as you can see, i added a print statement inside your which function, which prints out number1 and number, and as you can see they are 0. Why? well, you create new variables inside your function, so they don’t exist outside the function, to resolve this issue, you could return the number:

``````return number1
``````

and then when you call the function you store this in a variable:

``````number1 = first_number()
``````

for next time, use one of the two following options to make your code/indent is visible:

select your code and press ctrl + shift + c (or cmd + shift + c if you use a mac)

if this instructions are unclear, you can also insert 3 backticks before and after your code, like so:

`````````
<p>visible</p>
```
``````

the backtick is located above the tab key on your keyboard

Haha thank you! I have fixed the problem now, however i have been trying to find a way to build up a function that re-runs the code back from the top again, could you please explain how i achieve this? Many thanks for your replies

It is not that simple, and depends. Rerun it from the start, when? after it is finished? After a certain condition isn’t met? I actually think this is a really good challenge for you, i can help you, but you need to be specific in what you want

I just expirimented and it actually ended up in a success, this is what i have,

print(“Welcome to my calculator!”)

options = [",/,+,-,"]
number1 = int()
number2 = int()
def first_number():
number1 = int(input("Please enter the first number "))
return number1
def second_number():
number2 = int(input("Please enter the second number "))
return number2
def which(first_number, second_number):
print (options)
typeofsum = str(input("Which of the following would you like to add? -> “))
if typeofsum == (”
"):
print (number1 * number2)
elif typeofsum == ("/"):
print (number1 / number2)
elif typeofsum == ("+"):
print (number1 + number2)
elif typeofsum == ("-"):
print (number1 - number2)
else:
print (“No option assigned”)

def restart():
yes_or_no = str(input(“Would you like to add a new set of numbers? (Y/N)”))
if yes_or_no == (“Y”) or yes_or_no == (“y”):
number1 = first_number()
number2 = second_number()
which(number1, number2)
restart()
elif yes_or_no == (“N”) or yes_or_no == (“n”):
print (“Thank you for participating in my first ever program!”)
else:
print (“Sorry, i did not get that…”)
restart()

number1 = first_number()
number2 = second_number()
which(number1, number2)
restart()

nice code, i think you can optimize it if you look for an additional challenge

Yeah I’ll start to expiriment more, i noticed that when i divide 10 by 20 it gives an error saying you cant divide past 0…
This is my first ever programming language to learn, it’s going quite well but sometimes my mind messes up haha. I’m looking forward to study IT and i’ll need to learn Html and Java and C# ect, i’m only 16 now, this site has helped me a lot i must say Cheers for having an amazing community and website :3

1 Like

10/20 = 0.5, which will be 0. maybe you do want to use float numbers (numbers with decimal) instead

So i would need to put #number1 = float(number1)?#

That depends on how many changes you want to made, the only way you get a decimal number is is by dividing, so i think you could just do:

``````print (float(number1 / number2))
``````

Also, i just added the Main function, which just replaces:
number1 = first_number()

number2 = second_number()

which(number1, number2)

restart()

With the function that has exactly that inside.
but then i still have the 0 problem like before,

all my outcomes print 0 when i assign the calls of the functions and put them in a function called Main

def Main():
number1 = first_number()
number2 = second_number()
which(number1, number2)
restart()

Main()

I helped you on your way, don’t you think this is a nice challenge for you?

for next time, use one of the two following options to make your code/indent is visible:

select your code and press ctrl + shift + c (or cmd + shift + c if you use a mac)

if this instructions are unclear, you can also insert 3 backticks before and after your code, like so:

`````````
<p>visible</p>
```
``````

the backtick is located above the tab key on your keyboard

Haha okay thank you sir, appreciate the help :)!