#๐ TypeError: bad operand type for unary +: 'str'
61 messages ยท Page 1 of 1 (latest)
@elder vine
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.
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
!CODE
format it like that
, + Fore.RESET) this causes that error
!f-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.
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
how would i learn to do that?
- is not required, but it may be surprising to deal with implicit str concatenation
time.sleep(2)
for char in listreasons:
sys.stdout.write(char)
sys.stdout.flush()
time.sleep(0.1)```
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.
listreasons = f"To {Fore.RED}fight{Fore.RESET}, To {Fore.RED}learn{Fore.RESET}, To become one with the {Fore.GREEN}Matrix{Fore.RESET}"
you do both. look closer
f in front of a string indicates it's an "f-string"
that can take variables between {...}
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.")
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
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
yo man tysm
is there a way to make like a loading thing? So it would say Loading... but the "..." would be constantly deleting itself and retyping itself for a few seconds?
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
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?
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 ๐
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)```
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
put the function definition at the top of your file and in line 121 just call loading_simulator(50, 0.1)
as well as being on line 128?
no
do i put the definetion at line 1 or after the imports?
after imports
should be
works perfect ty 1 last thing
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
ty
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.