#๐Ÿ”’ TypeError: bad operand type for unary +: 'str'

61 messages ยท Page 1 of 1 (latest)

spiral copperBOT
#

@elder vine

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.

elder vine
#

listreasons = str("To " + Fore.RED + "fight" + Fore.RESET +"," "To " + Fore.RED + "learn" + Fore.RESET +"," "To become one with the " + Fore.GREEN + "Matrix", + Fore.RESET) time.sleep(2) for char in listreasons: sys.stdout.write(char) sys.stdout.flush() time.sleep(0.1) is my code

night oasis
#

!CODE

spiral copperBOT
#
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.

night oasis
#

format it like that

sly ether
#

, + Fore.RESET) this causes that error

wind wolf
#

!f-strings

spiral copperBOT
#
Format-strings

Creating a Python string with your variables using the + operator can be difficult to write and read. F-strings (format-strings) make it easy to insert values into a string. If you put an f in front of the first quote, you can then put Python expressions between curly braces in the string.

>>> snake = "pythons"
>>> number = 21
>>> f"There are {number * 2} {snake} on the plane."
"There are 42 pythons on the plane."

Note that even when you include an expression that isn't a string, like number * 2, Python will convert it to a string for you.

sly ether
#

and it also seems like you may be missing some +s between some of the strings. I would recomment using formatting, like the f-strings Sani posted

elder vine
#

how would i learn to do that?

dapper condor
#
  • is not required, but it may be surprising to deal with implicit str concatenation
elder vine
#
                                    time.sleep(2)
                                    for char in listreasons:
                                        sys.stdout.write(char)
                                        sys.stdout.flush()
                                        time.sleep(0.1)```
spiral copperBOT
#

Hey @elder vine!

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.
elder vine
#

so what should I do to correct the code?

#

oh i found it

#

i see wym thanks

sly ether
elder vine
#

why do I type f"

#

instead of Fore.RED

wind wolf
#

you do both. look closer

#

f in front of a string indicates it's an "f-string"

#

that can take variables between {...}

elder vine
#

whats that mean?

#

it works so far for what ive been typing

wind wolf
#

there is quite a readabilty difference between this:

print("There are " + str(number * 2) + " " + str(snake) + " on the plane.")
``` and this:
```py
print(f"There are {number * 2} {snake} on the plane.")
elder vine
#

oh alr ill try figure that out

#

would there be a way i could make like a rainbow bar? as in it would be RGB -------------------------------

#

like that but obviously it would be ------------------------------ instead of a glowing stick

sly ether
# elder vine would there be a way i could make like a rainbow bar? as in it would be RGB ----...
from colorama import Fore

my_string = "-------------------------------"
# get a list of Colorama colors (excluding RESET)
colors = [getattr(Fore, color) for color in dir(Fore) if not color.startswith('_') and color != 'RESET']

def print_rainbow(text):
    color_count = len(colors)
    for i, char in enumerate(text):
        print(colors[i % color_count] + char, end='')
    # without this I had a % appear at the end of my printed string
    print()

print_rainbow(my_string)
``` the best I could figure out, not sure if there is a function for that
elder vine
#

yo man tysm

elder vine
sly ether
#

yeah there is, you basically cycle between Loading. Loading.. Loading..., deleting the text in the command line between each step which happens so quickly it looks like there is just a new dot appearing

elder vine
#

is there a quicker way to do it using loops or should i just do print("Loading." print("Loading..") etc

#

wait no that would make a new line

#

how would I write this?

sly ether
# elder vine how would I write this?

for example a cool loading bar that shows how much is left to do (steps) would be

import time, sys

def loading_simulator(steps: int, delay: float):
    for i in range(steps):
        sys.stdout.write(f"\r[{'=' * i}{' ' * (steps - i)}] {i + 1}/{steps}")
        sys.stdout.flush()
        time.sleep(delay)
    sys.stdout.write("\n")

loading_simulator(50, 0.1)
``` to do your thing, for example
```py
import time, sys

def loading_simulator(steps: int, delay: float):
    for i in range(steps):
        sys.stdout.write(f'\rLoading{"." * i}')
        sys.stdout.flush()
        time.sleep(delay)
    print()

loading_simulator(5, 0.1)
``` but I'm struggling to make it cycle through the dots ๐Ÿ˜…
elder vine
#

I'll try this code

#

ty btw

elder vine
#

how would i implement the code? ```import time, sys

def loading_simulator(steps: int, delay: float):
for i in range(steps):
sys.stdout.write(f"\r[{'=' * i}{' ' * (steps - i)}] {i + 1}/{steps}")
sys.stdout.flush()
time.sleep(delay)
sys.stdout.write("\n")

loading_simulator(50, 0.1)```

spiral copperBOT
#

Hey @elder vine!

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.
sly ether
#

not sure what you're asking, you can just run that

sly ether
#

put the function definition at the top of your file and in line 121 just call loading_simulator(50, 0.1)

sly ether
#

no

elder vine
sly ether
#

after imports

elder vine
#

so thats all i need?

#

at the bottom

sly ether
#

should be

elder vine
#

i have like a thing where it has input and then u type in a password, is there anyway i can make it so the password will say ***** instead of the actual password

sly ether
elder vine
#

ty

spiral copperBOT
#
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.