#๐Ÿ”’ Hey guys, day one of learning python and im trying to build a calculattor.

10 messages ยท Page 1 of 1 (latest)

urban hollow
#

somehow, when i pick the option "sqrt" it does works but shows the second "else" too... how can i fix it?

#Trying to build a calculator

import math

n1=float(input('Pick a number:'))
opr=str(input('+, -, *, /, **, sqrt?'))
if opr == 'sqrt':
    s=math.sqrt(n1)
    print(f'The square root of {n1} is {s}')
else:
    n2=float(input('Another one:'))

if opr == '+':
    s=n1+n2
    print(f'The sum of {n1} and {n2} is equal  to {s}')
    
elif opr == '-':
    s=n1-n2
    print(f'The subtraction between {n1} and {n2} is equal to {s}')
    
elif opr == '*':
    s=n1*n2
    print(f'The product between {n1} and {n2} is equal to {s}')
    
elif opr == '/':
    s=n1/n2
    print(f'The division between {n1} and {n2} is equal to {s}')
    
elif opr == '**':
    s=n1**n2
    print(f'{n1} to the power of {n2} is equal to {s}')
    
else:
    print('Are you dumb? Please pick a valid operation/number!')

if i run (number) then sqrt, it shows:
Pick a number:
55
+, -, *, /, **, sqrt?
sqrt
The square root of 55.0 is 7.416198487095663 (first else)
Are you dumb? Please pick a valid operation/number (second else)

sick spokeBOT
#

Hey @urban hollow!

It looks like you pasted Python code without syntax highlighting.

Please use syntax highlighting to improve the legibility of your code and make it easier for us to help you.

To do this, use the following method:
```py
print('Hello, world!')
```

This will result in the following:

print('Hello, world!')```
You can **edit your original message** to correct your code block.
sick spokeBOT
#

@urban hollow

Python help channel opened

Remember to:

  • Ask your Python question, not if you can ask or if there's an expert who can help.
  • Show a code sample as text (rather than a screenshot) and the error message, if you've got one.
  • Explain what you expect to happen and what actually happens.

:warning: Do not pip install anything that isn't related to your question, especially if asked to over DMs.

slow kestrel
#

you'll want to indent the entire second if-chain to be inside the first "else"

#

since you want it to only happen with the 2nd operand that you take there

knotty vector
#

you can also exit() in the if opr == 'sqrt': block.

urban hollow
#

Thanks guys! Love ya

sick spokeBOT
#
Python help channel closed

This help channel has been closed and it's no longer possible to send messages here. If your question wasn't answered, feel free to create a new post in #1035199133436354600. To maximize your chances of getting a response, check out this guide on asking good questions.