#๐Ÿ”’ I'm stuck on my code

81 messages ยท Page 1 of 1 (latest)

proven eagleBOT
#

@vale sundial

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.

short haven
#

!code

proven eagleBOT
#
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.

vale sundial
#

I was watching a tutorial where we were making a restaurant and we needed to convert a string to an integer

#

But when I try to I get an error

short haven
#

you can use int() to convert from string to integer

#

what error do you get?

vale sundial
#

TypeError: can only concatenate str (not "int") to str

short haven
#

the full error

timber marsh
#

on that line, (the variable should be marked in the traceback) you're adding a number and a string together, which python forbids because that just doesn't make sense

vale sundial
#

the tutorial guy did it and it worked

short haven
#

show us your code

vale sundial
#

ok

timber marsh
vale sundial
#

just send it?

short haven
#

yeah

timber marsh
#

if it involves a hint of black magic

timber marsh
proven eagleBOT
#
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.

timber marsh
#

in here

short haven
#

its also fine to paste in discord

#

if its not too big

timber marsh
#

i can't see syntax highlighting

#

that's why i said to use the pastebin regardless

short haven
#

!code

proven eagleBOT
#
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.

timber marsh
#

like i said

short haven
#

discord has builtin syntax highlighting

timber marsh
#

i can't see syntax highlighting

timber marsh
short haven
#

oh i didnt know that

timber marsh
#

yep. awful state of affairs

vale sundial
#

do I send the code file or what

timber marsh
#

whole point of a pastebin is that you share files by link

vale sundial
#

ohh

short haven
#

paste it in the pastebin and then sent the link

vale sundial
short haven
#

yeah

timber marsh
#

i recommend you use f strings for this instead

vale sundial
#

yeah?

#

f strings?

timber marsh
#

string interpolation is a common technique

timber marsh
vale sundial
#

sorry I'm brand new to Python could you tell me how to write it

timber marsh
#

an f-string basically just formats the variables inside it into whatever you're working with

short haven
#

print(f"That will be ${total}!")

#

like this

timber marsh
proven eagleBOT
timber marsh
#

it automatically converts the variables into strings so you don't run into this issue

vale sundial
#

ok thanks guys

timber marsh
#

you can also use format specifiers (more advanced) like printing a number with comma separators ({x:,}), printing to 2 decimal places ({x:.2f}) and more

timber marsh
#

!res

proven eagleBOT
#
Resources

The Resources page on our website contains a list of hand-selected learning resources that we regularly recommend to both beginners and experts.

timber marsh
#

here are better resources

#

seems like your tutorial isn't that good

vale sundial
#

ok

short haven
#

you could also set the variable to a string total = str((price * int(number)))

short haven
#

but f-strings are easier

timber marsh
#

if you wanted to add to the total, you'd have to turn it BACK into an integer, update the value, then make it a string again

short haven
#

i just wanted to give him another option
its good to know things like this

#

but yeah i agree with you

timber marsh
#

if you had an issue with a car that you needed to get to work that was 30 miles away, it wouldn't be a good idea to be recommended a bicycle

short haven
#

yeah but i just wanted to mention it

timber marsh
short haven
#

i said thats its easier to use f-strings

timber marsh
#

ah ok

#

@vale sundial close the thread with !close

vale sundial
#

Oh ok

#

!close

proven eagleBOT
#
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.