You are not logged in.
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 .
Thanks for advice or help.
Last edited by SpeedVin (2009-09-22 18:18:58)
Shell Scripter | C/C++/Python/Java Coder | ZSH
Offline
Could you be more specific? What do you have so far and what's your programm supposed to do?
Offline
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
Shell Scripter | C/C++/Python/Java Coder | ZSH
Offline
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 Stuff • Forum Etiquette • Community Ethos - Arch is not for everyone
Offline
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
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.
*edited for typos*
Last edited by Xyne (2009-09-22 18:15:17)
My Arch Linux Stuff • Forum Etiquette • Community Ethos - Arch is not for everyone
Offline
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.
*edited for typos*
Thanks Xyne it's so simple!
Shell Scripter | C/C++/Python/Java Coder | ZSH
Offline
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
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
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
My Arch Linux Stuff • Forum Etiquette • Community Ethos - Arch is not for everyone
Offline