#๐Ÿ”’ trying to make a calculator

116 messages ยท Page 1 of 1 (latest)

zenith dagger
#

i use pycharm since yt said it was the best for beginners. but when i try to combine the adding and subtracting options it fails
my code:
print('you can choose between 4 options: '
'adding: 1, '
'substraction: 2, , '
'division: 3, '
'multiplication: 4')
form = int(input('choose an option: '))
if form == 1: num1 = input('choose a number: ')
num2 = input('choose another one: ')
result = float(num1) + float(num2)

print(result)

if (form == 2): num3 = input('choose a number: ')
num4 = input('choose another one: ')
result = float(num3) - float(num4)

print(result)

keeps saying num1 or num 3 is undefined but what does that mean?

marsh hawkBOT
#

@zenith dagger

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.

abstract mural
#

When you have an if statement, you need to indent the code beneath it.

pallid aurora
#

I WAS GOING TO SAY THAT

abstract mural
#

Like I said already, please don't shout

#

Anyway, indentation is important in Python.

pallid aurora
#

anyways yea, indent is one of the most important things of this language

pallid aurora
abstract mural
#

The whitespace is the same thing as some curly braces in C++ or javascript

pallid aurora
#

no extra indentation, no missing indentation

abstract mural
#

So, the reason your editor is raising these flags is because you haven't indented the lines where you are initialising num1 and num3.

pallid aurora
abstract mural
#

I would recommend adding a new line after the if statement too

#

Doing if form == 1: num1 = input('choose a number: ') in one line isn't really recommended.

#

Instead,

if form == 1:
  # Indent using Tab
  num1 = input('choose a number: ')
zenith dagger
#

oh

#

nvm

pallid aurora
zenith dagger
#

alr

#

so where do i put that

abstract mural
#

After the if statement.

zenith dagger
#

the indent

#

okay

pallid aurora
#

before every line in the if statement

abstract mural
#

Whenever you want something to run after the if statement, indent those lines.

pallid aurora
abstract mural
#

So instead your code might look a little like

if form == 1: 
    num1 = input('choose a number: ')
    num2 = input('choose another one: ')
    result = float(num1) + float(num2)
    print(result)
zenith dagger
#

ah

#

yes

#

ill try to run it

#

YES

#

it works

#

thank you

abstract mural
#

One small thing, you don't really have to print result each time.

zenith dagger
#

oh huh

#

wdym

abstract mural
#

Instead, you can make it look a little bit like

form = int(input('choose an option: '))
if form == 1: 
    #code

if (form == 2): 
    #code

print(result)
pallid aurora
#
print('you can choose between 4 options: '
      'adding: 1,  '
      'substraction: 2, , '
      'division: 3, '
      'multiplication: 4')
form = int(input('choose an option: '))
num1 = int(input('first: '))
num2 = int(input('second: '))
match form:
    case 1:
        print(num1 + num2)
    case 2:
        print(num1 - num2)
    case 3:
        ...

this will be better

abstract mural
pallid aurora
#

why...you...use...ellipsis...all...over

abstract mural
#

Especially if indentation is an issue that you are having. Just ignore it for now.

pallid aurora
#

basically same as if elif elif elif ...

zenith dagger
#

aha

abstract mural
zenith dagger
#

alr

pallid aurora
#

yea but it is really important in otehr languages in C and JS which is named switch~case for some reason

violet night
#

It would be named differently in those cases because it's different in C and JS. In those languages, switch does exact == checks to decide which branch to follow. Python's match uses pattern matching.

pallid aurora
#

๐Ÿ‘

zenith dagger
#

sounds important

pallid aurora
#

if you know it you know it)

abstract mural
zenith dagger
#

alr

#

i made the calculator work

violet night
# zenith dagger i have no idea what this all means lmao

In other languages, switch can "jump" to the correct case without needing to check all the previous ones, which is more efficient. In Python, match is basically just an if statement.

But yes, I wouldn't worry about match yet.

pallid aurora
#

sorry i didnt get your skill lvl

zenith dagger
#

like

#

fully

abstract mural
#

It's crucial in Python, especially when you start to nest things.

pallid aurora
pallid aurora
#

in otehr languages

pallid aurora
#

repeating that

zenith dagger
#

oh damn

#

anyway

#

what should i try next to expand my skills

abstract mural
pallid aurora
#

so

if something:
    if something2:
        if something3:
            if something4:
                # go on
zenith dagger
#

quick question how do yall make the weird black boxes with code

marsh hawkBOT
#
Formatting code on Discord

Here's how to format Python code on Discord:

```py
print('Hello world!')
```

These are backticks, not quotes. Check this out if you can't find the backtick key.

For long code samples, you can use our pastebin.

abstract mural
#

Read the embed.

pallid aurora
#

f

#

wait why doesnt those work

#

!paste

marsh hawkBOT
#
Pasting large amounts of code

If your code is too long to fit in a codeblock in Discord, you can paste your code here:
https://paste.pythondiscord.com/

After pasting your code, save it by clicking the Paste! button in the bottom left, or by pressing CTRL + S. After doing that, you will be navigated to the new paste's page. Copy the URL and post it here so others can see it.

pallid aurora
#

what???

abstract mural
#

The backtick is the key under the escape.

pallid aurora
#

then whyd that not work

abstract mural
pallid aurora
#

oh ok

zenith dagger
#

so you put !code and you put a bunch of code behind it?

pallid aurora
#

uh no.

abstract mural
zenith dagger
#

oh yea

pallid aurora
zenith dagger
#

i see it

#

so

pallid aurora
#

ok i am going to sleep

#

bye

zenith dagger
#

py print( result )

abstract mural
zenith dagger
#

ye

#

hmm

abstract mural
#

Make sure the ```py

zenith dagger
#

so

#

py print( result )

#

no

#

huh

abstract mural
#

No

#

```py

#

You need a newline after ``

zenith dagger
#
```print( result )```
#

almost

#
print( result )```
#

yes

marsh hawkBOT
#
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.