Python Forum
Very basic calculator question
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Very basic calculator question
#1
Dear people, i have recently started learning Python (about a week ago) and i am trying to write a calculator.
At first i made a simple one, to do adding up. But then i wanted to give the user the option to choose between + - or /.
I get to the point where everything adds up to be 10+10 example, instead of 20.

this is my code:

   

this is what i get when i run it:

   

How do i make it go from 10+10 to 20?
I've already tried changing str to int or float, but it gave an error.

Thank you!

Boudewijn
Yoriz write Dec-09-2021, 08:41 PM:
Please post all code, output and errors (in their entirety) between their respective tags. Refer to BBCode help topic on how to post. Use the "Preview Post" button to make sure the code is presented as you expect before hitting the "Post Reply/Thread" button.
Reply
#2
Please post your code, not screenshots.

Your code concatenates strings. If you type in +, 1, 2 then sum is going to be "1+2". Sounds like you want to to do is add the numerical values to produce the sum 3.

Changing first and second to floats is a good start, but what are you going to do for choice? How can you code add two numbers when choice is "+", subtract second from first when choice is "-", compute the product when choice is "*" and the quotient when choice is "/"?
Reply
#3
Have a look at eval

One way could be

operators = ['+', '-', '*', '/']
choose = input(f'Choose Operator: {operators} ')
num1 = float(input('num1 >> '))
num2 = float(input('num2 >> '))
total = f'{num1}{choose}{num2}'
print(eval(total))
Output:
Choose Operator: ['+', '-', '*', '/'] + num1 >> 5 num2 >> 3 5.0+3.0 = 8.0
I welcome all feedback.
The only dumb question, is one that doesn't get asked.
My Github
How to post code using bbtags


Reply
#4
Eval is lazy, dangerous and evil. Well maybe not evil, but definately lazy and dangerous. Figure out how your program can take appropriate actions based on the choice input.
ndc85430 likes this post
Reply
#5
Thank you everybody!
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Basic Coding Question: Exit Program Command? RockBlok 3 579 Nov-19-2023, 06:31 PM
Last Post: deanhystad
  [solved] Basic question on list matchiing paul18fr 7 1,874 May-02-2022, 01:03 PM
Last Post: DeaD_EyE
  New to python, trying to make a basic calculator AthertonH 2 1,148 Apr-14-2022, 03:33 PM
Last Post: AthertonH
  basic question isinstance tames 5 2,844 Nov-23-2020, 07:20 AM
Last Post: tames
  basic question about tuples and immutability sudonym3 6 2,917 Oct-18-2020, 05:11 PM
Last Post: sudonym3
  Basic Pyhton for Rhino 6 question about variables SaeedSH 1 2,156 Jan-28-2020, 04:33 AM
Last Post: Larz60+
  Basic Beginner question NHeav 4 2,788 Sep-13-2019, 11:43 AM
Last Post: NHeav
  Basic coding question with Python Than999 3 3,120 Jul-17-2019, 04:36 PM
Last Post: jefsummers
  basic question???????? cemdede 2 2,356 Jan-18-2019, 03:05 AM
Last Post: ntd9395
  basic plotting question Devilish 0 1,903 Dec-27-2018, 10:35 PM
Last Post: Devilish

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020