You are not logged in.

#1 2009-09-22 15:06:34

SpeedVin
Member
From: Poland
Registered: 2009-04-29
Posts: 955

[Solved]A little calculator writed in Python.

Hello.
I started to write programs in Python and I really like it (Python).
i want to start programming in Python from make a little calculator program (You can use it without X).
And I got all things but I don't know how to write a action that python will read + or - I writedhow to input a nuber to program but I don't know how input + to my program neutral.
Thanks for advice or help.

Last edited by SpeedVin (2009-09-22 18:18:58)


Shell Scripter | C/C++/Python/Java Coder | ZSH

Offline

#2 2009-09-22 15:13:16

g0ju
Member
From: Chemnitz, Germany
Registered: 2009-08-08
Posts: 23

Re: [Solved]A little calculator writed in Python.

Could you be more specific? What do you have so far and what's your programm supposed to do?

Offline

#3 2009-09-22 15:20:14

SpeedVin
Member
From: Poland
Registered: 2009-04-29
Posts: 955

Re: [Solved]A little calculator writed in Python.

g0ju wrote:

Could you be more specific? What do you have so far and what's your programm supposed to do?

He have to calculate.
2 + 3
or
2 * 3
For now he understand  how to read inputed numbers but don't know about how to read sign like I input 2 as first number and 2 as second and I want to sum that 2 numbers wink


Shell Scripter | C/C++/Python/Java Coder | ZSH

Offline

#4 2009-09-22 15:31:24

Xyne
Administrator/PM
Registered: 2008-08-03
Posts: 6,965
Website

Re: [Solved]A little calculator writed in Python.

It depends on how you're getting user input. Are you accepting arguments on the command line or are you reading them interactively in your code?


It would help if you posted the actual code that you have already so that we can see what you're doing. General descriptions are usually not very useful when trying to help someone with code.


My Arch Linux StuffForum EtiquetteCommunity Ethos - Arch is not for everyone

Offline

#5 2009-09-22 16:56:04

SpeedVin
Member
From: Poland
Registered: 2009-04-29
Posts: 955

Re: [Solved]A little calculator writed in Python.

That's the code of my program:

  1 #!/usr/bin/env python
  2 # -*- coding: utf-8 -*-
  3 
  4 number1 = int(raw_input('Write a number: '))
  5 number2 = int(raw_input('Write a number nr.2: '))
  6 znak = int(raw_input('What to do?'))
  7 print number1 + number2

Last edited by SpeedVin (2009-09-22 17:01:03)


Shell Scripter | C/C++/Python/Java Coder | ZSH

Offline

#6 2009-09-22 18:12:01

Xyne
Administrator/PM
Registered: 2008-08-03
Posts: 6,965
Website

Re: [Solved]A little calculator writed in Python.

Remove the "int()" call that's wrapping the third input. "int()" converts its argument to an integer.

Try something like this:

#!/usr/bin/env python
# -*- coding: utf-8 -*-

number1 = int(raw_input('Write a number: '))
number2 = int(raw_input('Write a number nr.2: '))
znak = raw_input('What to do?')
if znak == '+':
    print number1 + number2
elif znak == '-':
    print number1 - number2
else:
    print "I don't know what to do with " + znak

Of course this would be impractical as the number of operations increases, but discovering a better way to do it is part of the learning experience. wink

*edited for typos*

Last edited by Xyne (2009-09-22 18:15:17)


My Arch Linux StuffForum EtiquetteCommunity Ethos - Arch is not for everyone

Offline

#7 2009-09-22 18:18:32

SpeedVin
Member
From: Poland
Registered: 2009-04-29
Posts: 955

Re: [Solved]A little calculator writed in Python.

Xyne wrote:

Remove the "int()" call that's wrapping the third input. "int()" converts its argument to an integer.

Try something like this:

#!/usr/bin/env python
# -*- coding: utf-8 -*-

number1 = int(raw_input('Write a number: '))
number2 = int(raw_input('Write a number nr.2: '))
znak = raw_input('What to do?')
if znak == '+':
    print number1 + number2
elif znak == '-':
    print number1 - number2
else:
    print "I don't know what to do with " + znak

Of course this would be impractical as the number of operations increases, but discovering a better way to do it is part of the learning experience. wink

*edited for typos*

Thanks Xyne it's so simple! wink


Shell Scripter | C/C++/Python/Java Coder | ZSH

Offline

#8 2009-09-22 18:19:06

XFire
Member
From: UK
Registered: 2008-05-11
Posts: 192

Re: [Solved]A little calculator writed in Python.

It would be better if you requested the equation as a string, then by using regular expressions, deem what is required of the program. Alternatively, you could do something similar but tokenise the string, splitting it up into first number, the operator and then the second number.


There is a difference between bleeding [edge] and haemorrhaging. - Allan

Offline

#9 2009-09-22 18:24:53

Barrucadu
Member
From: York, England
Registered: 2008-03-30
Posts: 1,158
Website

Re: [Solved]A little calculator writed in Python.

XFire wrote:

It would be better if you requested the equation as a string, then by using regular expressions, deem what is required of the program. Alternatively, you could do something similar but tokenise the string, splitting it up into first number, the operator and then the second number.

I'd just use eval().

Offline

#10 2009-09-22 19:16:13

Xyne
Administrator/PM
Registered: 2008-08-03
Posts: 6,965
Website

Re: [Solved]A little calculator writed in Python.

I thought of eval too, but that introduces security issues (same as "input").
You could even argue that you can skip the code and use python's shell directly, but that defeats the purpose of learning.

I had some other thoughts too, but I'm leaving them unsaid. This is a one-man bikeshed until the next problem arises smile


My Arch Linux StuffForum EtiquetteCommunity Ethos - Arch is not for everyone

Offline

Board footer

Powered by FluxBB