Jump to content

First time making a calc

Marvzl1357

Hullo, 

Anyway, I started poking at programming two to three days ago and decided I should make a calculator.

After some failure due to an input being taken as a string, I figured out how to make an input a number.

This "calculator" works for simple calculations (+-/*) of two numbers only. 

*Python

print ("Calculatorz")a=raw_input("Num 1 ")f=raw_input("Funct ")b=raw_input("Num 2 ")aa=float (a)bb=float (b)if f == "+":    print (aa+bb)elif f == "-":    print (aa-bb)elif f == "*":    print (aa*bb)elif f == "/":    print (aa/bb)else:    print "NaN"

Anyway, thanks for reading this, sorry for being noobish, but I was overjoyed when I figured this out. 

Also, I'm not sure, and can't really find how, to just loop the whole lot of code so it doesn't instantly end after doing its calculations. Anyone care to help?

Home is where the heart my desktop is.

Link to comment
Share on other sites

Link to post
Share on other sites

If you want to loop the program forever, until you close the console or window you can insert the code into a  'while True' tag like this: 

Nah, didn't work, I was hoping something like:

 

x=1

while True:

[run calc program]

 

But I don't know how to do that, and it sure is a lazy way to circumvent the problem

Home is where the heart my desktop is.

Link to comment
Share on other sites

Link to post
Share on other sites

Nah, didn't work, I was hoping something like:

 

x=1

while True:

[run calc program]

 

But I don't know how to do that, and it sure is a lazy way to circumvent the problem

This is basically what the other person said, but his indent level was incorrect which may give you an error, if you just want a loop, you can do this:

while True:    print ("Calculatorz")    a=raw_input("Num 1 ")    f=raw_input("Funct ")    b=raw_input("Num 2 ")         aa=float (a)    bb=float (b)         if f == "+":        print (aa+bb)    elif f == "-":        print (aa-bb)    elif f == "*":        print (aa*bb)    elif f == "/":        print (aa/bb)    else:        print "NaN" 

Intel 3570K - MSI GTX 660Ti 3GB OC Edition - 16GB Corsair LP RAM - ASRock Extreme4 Motherboard - Corsair HX850 - Adata Premier Pro SP900 120GB SSD with Windows 7 - Seagate Barracuda 1TD HDD - Seagate Barracuda 500GB HDD - Thermaltake Frio CPU Cooler - CM Storm Enforcer Case - Macbook Pro Early 2011 Laptop

Link to comment
Share on other sites

Link to post
Share on other sites

while True:    print ("Calculatorz")    a=raw_input("Num 1 ")    f=raw_input("Funct ")    b=raw_input("Num 2 ")     aa=float (a)    bb=float (b)     if f == "+":        print (aa+bb)    elif f == "-":        print (aa-bb)    elif f == "*":        print (aa*bb)    elif f == "/":        print (aa/bb)    else:        print "NaN"

Or to be more like what you wanted

def calc():    print ("Calculatorz")    a=raw_input("Num 1 ")    f=raw_input("Funct ")    b=raw_input("Num 2 ")     aa=float (a)    bb=float (b)     if f == "+":        print (aa+bb)    elif f == "-":        print (aa-bb)    elif f == "*":        print (aa*bb)    elif f == "/":        print (aa/bb)    else:        print "NaN"while True:    calc()

1474412270.2748842

Link to comment
Share on other sites

Link to post
Share on other sites

 

This is basically what the other person said, but his indent level was incorrect which may give you an error, if you just want a loop, you can do this:

 

Yeah, that's exactly what I was looking for, thanks a bunch, now I have a usable, err... two digit calculator! :P

Home is where the heart my desktop is.

Link to comment
Share on other sites

Link to post
Share on other sites

speaking of infinite loops, uh

I would argue that it isn't an infinite loop.....you could always enter 1 / 0 to exit the loop with a crash :P *I know it still is an infinite loop though*

 

btw Marvzl1357 you might want to check for division that bb doesn't equal zero

0b10111010 10101101 11110000 00001101

Link to comment
Share on other sites

Link to post
Share on other sites

I would argue that it isn't an infinite loop.....you could always enter 1 / 0 to exit the loop with a crash :P *I know it still is an infinite loop though*

 

I would argue that this is very bad programming (and that it it still is an infinite loop).

Mini-Desktop: NCASE M1 Build Log
Mini-Server: M350 Build Log

Link to comment
Share on other sites

Link to post
Share on other sites

I would argue that this is very bad programming (and that it it still is an infinite loop).

Oh yea, definitely agree....the error should really be caught, and have an exit code :P

 

I am not familiar enough with Python to now this result...but what if you enter "one * cat" would it actually throw an error or just continue on assuming the numbers to be 0 and 0?  Anyways just a thing to consider, to check valid user inputs as well

0b10111010 10101101 11110000 00001101

Link to comment
Share on other sites

Link to post
Share on other sites

Anyways just a thing to consider, to check valid user inputs as well

geez, users are so annoying

 

when i program 'for fun' i just assume that this guy will be the only user

cWk2VTdiSzA1ZWMx_o_good-guy-greg.jpg

Link to comment
Share on other sites

Link to post
Share on other sites

geez, users are so annoying

 

when i program 'for fun' i just assume that this guy will be the only user

cWk2VTdiSzA1ZWMx_o_good-guy-greg.jpg

haha, yea I don't practice what I preach :P I had a "just for me" program once (a program to clean up my temporary files)....my brother got on my computer and used it for fun....wiped out an entire directory :P

0b10111010 10101101 11110000 00001101

Link to comment
Share on other sites

Link to post
Share on other sites

dear gawd python is a horrible language....

 

but ya, general rule: 50% of your program will be checking the user's input

seriously. don't take it literally, just be aware that user input is a huge component of all non-trivial programs.

 

the next 'assignment' is to make the calculator accept the 'exit' string to close the program.  this tests the student in a few ways:

 

1.  change the initial input variable to something more generic (like a string) rather than hard coding it to a numeric type

2.  converting data from one type to another

3.  teaches the student to not use infinite loops

Link to comment
Share on other sites

Link to post
Share on other sites

dear gawd python is a horrible language....

 

but ya, general rule: 50% of your program will be checking the user's input

seriously. don't take it literally, just be aware that user input is a huge component of all non-trivial programs.

 

the next 'assignment' is to make the calculator accept the 'exit' string to close the program.  this tests the student in a few ways:

 

1.  change the initial input variable to something more generic (like a string) rather than hard coding it to a numeric type

2.  converting data from one type to another

3.  teaches the student to not use infinite loops

Just out of curiosity why do you hate python? It is a slow language, but it is great for a first language, it is so easy to learn (and very simple).

 

as for the checking, a good way to do that for this program would be:

 

while True:    error = False    print ("Calculatorz")    a=raw_input("Num 1 ")    f=raw_input("Funct ")    b=raw_input("Num 2 ")     try:        aa=float (a)        bb=float (b)     except:        if a == 'exit' or 'b' == 'exit' or 'f' == 'exit':            break        else:            error = True                if error == False:        if f == "+":            print (aa+bb)        elif f == "-":            print (aa-bb)        elif f == "*":            print (aa*bb)        elif f == "/" and bb != 0:            print (aa/bb)        else:            print "NaN" 

Intel 3570K - MSI GTX 660Ti 3GB OC Edition - 16GB Corsair LP RAM - ASRock Extreme4 Motherboard - Corsair HX850 - Adata Premier Pro SP900 120GB SSD with Windows 7 - Seagate Barracuda 1TD HDD - Seagate Barracuda 500GB HDD - Thermaltake Frio CPU Cooler - CM Storm Enforcer Case - Macbook Pro Early 2011 Laptop

Link to comment
Share on other sites

Link to post
Share on other sites

 

Just out of curiosity why do you hate python? It is a slow language, but it is great for a first language, it is so easy to learn (and very simple).

 

syntax failure upon incorrect indentation.

 

that means if someone's source control or editor modifies whitespacing.  it will break.  obviously the problem is fixable, but its an artificial problem that shouldn't be have been made in the first place.  as far as i know, it's the only modern language that requires proper indentation for scoping.

 

 

errors (which will ALWAYS happen) that you can see are far preferable to others that you can't

Link to comment
Share on other sites

Link to post
Share on other sites

Jesus, so many replies...

I'll give everything a shot soon, I haven't learnt about exceptions yet, only third day of actually doing productive stuff in python :P

Thanks for your help.

Home is where the heart my desktop is.

Link to comment
Share on other sites

Link to post
Share on other sites

Jesus, so many replies...

I'll give everything a shot soon, I haven't learnt about exceptions yet, only third day of actually doing productive stuff in python :P

Thanks for your help.

Just so you know, try except loops are very easy to use and helpful (they are in my last reply) Basically whatever you put in try it does, and if an error occurs (like if you do float('test') it will say that 'test' is not a number) it will stop the error from crashing the program and do whatever you tell it to instead (in my reply I tell it to set error to True)

Intel 3570K - MSI GTX 660Ti 3GB OC Edition - 16GB Corsair LP RAM - ASRock Extreme4 Motherboard - Corsair HX850 - Adata Premier Pro SP900 120GB SSD with Windows 7 - Seagate Barracuda 1TD HDD - Seagate Barracuda 500GB HDD - Thermaltake Frio CPU Cooler - CM Storm Enforcer Case - Macbook Pro Early 2011 Laptop

Link to comment
Share on other sites

Link to post
Share on other sites

Just so you know, try except loops are very easy to use and helpful (they are in my last reply) Basically whatever you put in try it does, and if an error occurs (like if you do float('test') it will say that 'test' is not a number) it will stop the error from crashing the program and do whatever you tell it to instead (in my reply I tell it to set error to True)

Ok

Just checking, what does error = True mean? does it just tell the program "That's wrong. Skip it" ?

Home is where the heart my desktop is.

Link to comment
Share on other sites

Link to post
Share on other sites

Ok

Just checking, what does error = True mean? does it just tell the program "That's wrong. Skip it" ?

What error = True does is set the word error to True, since later on I say "if error == False:" it will skip all of the code in that if statement You need to put the if statement there for it to work. Be sure to keep in mind that "error" if a variable I created, not a python built in function. So you could literally change error in my code to "purpleGiraffe" and it would work just as well.

Intel 3570K - MSI GTX 660Ti 3GB OC Edition - 16GB Corsair LP RAM - ASRock Extreme4 Motherboard - Corsair HX850 - Adata Premier Pro SP900 120GB SSD with Windows 7 - Seagate Barracuda 1TD HDD - Seagate Barracuda 500GB HDD - Thermaltake Frio CPU Cooler - CM Storm Enforcer Case - Macbook Pro Early 2011 Laptop

Link to comment
Share on other sites

Link to post
Share on other sites

What error = True does is set the word error to True, since later on I say "if error == False:" it will skip all of the code in that if statement You need to put the if statement there for it to work. Be sure to keep in mind that "error" if a variable I created, not a python built in function. So you could literally change error in my code to "purpleGiraffe" and it would work just as well.

Ah, that makes sense, but the True and False has to be capitalised for it to work as intended right? The first time I typed it in I didn't have capital True/False, and it returned an error.

Home is where the heart my desktop is.

Link to comment
Share on other sites

Link to post
Share on other sites

Ah, that makes sense, but the True and False has to be capitalised for it to work as intended right? The first time I typed it in I didn't have capital True/False, and it returned an error.

Correct, True and False are built into python, while something like 'true' and 'false' would be created by a coder (although I would not use those names because they are so close to True/False) 

Intel 3570K - MSI GTX 660Ti 3GB OC Edition - 16GB Corsair LP RAM - ASRock Extreme4 Motherboard - Corsair HX850 - Adata Premier Pro SP900 120GB SSD with Windows 7 - Seagate Barracuda 1TD HDD - Seagate Barracuda 500GB HDD - Thermaltake Frio CPU Cooler - CM Storm Enforcer Case - Macbook Pro Early 2011 Laptop

Link to comment
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×