Is there something wrong with this line of Python code?

2024/9/22 1:37:03

I tried this line of code, and it kept giving me the SyntaxError.

print('/' / '-' / '\' / '|' * 30, end='\r')^

It pointed on the brackets.
Any suggestions? Thanks!

Answer

As already mentioned in comments, the immediate problem is the syntax error caused by the un-escaped \, causing the \ to escape the following '. Next, there is the problem of / applied to strings... Python does not really know what you are trying to achieve with that.

From your choice of characters and the end="\r", I guess that you are trying to create some kind of "spinner" animation. For this, you could use modulo % to access the matching character, then use a proper for loop to print them. Also, you might have to flush the print stream besides resetting it to the start of the line, and you might want to add some waiting time in between.

import time
for i in range(30):print("/-\\|"[i%4], end="\r", flush=True)time.sleep(0.3)
https://en.xdnf.cn/q/119828.html

Related Q&A

TypeError: tuple indices must be integers or slices, not str postgres/python

Hi Im trying to get a timestamp from a row called time in my postgres database, however Im getting the error. Its the second row in the database. The error TypeError: tuple indices must be integers or …

Pandas Python: KeyError Date

I am import into python where it will automatically create a date time object.However I want the first column to be a datetime object in Python. Data looks likeDate,cost 41330.66667,100 41331.66667,101…

How to resample 1 minute data into 15 minute data?

CSV file. df before resample and after applying: df["dateandtime"] = (pd.to_datetime(df.pop("DATE").str.cat(df.pop("TIME"), sep=" "))) df = df.set_index(pd.Datet…

Python Pygame Lighting for Pong

Hey Guys Im writing a little Pong-Game in Pygame and wanted to use a glowing-effect on the Ball and the Bats. But Pygame dosent support this effects and make solid blocks out of it. Is there a way to h…

python - List.remove method not applicable as the function with the map builtin?

Question Can List.remove not be used as the function to apply in the map(function, iterable, ...) builtin? %%timeit import random m = _max = 10000 n = random.randint(1, m)outer = random.sample(populat…

How do I change a sum for string for it to work?

This is my whole program Im working on (with the function not in the right place due to the code needing to be indented) but anyway there is a problem that Im not sure how to fix.How do I change it so …

Convert type str to numerator/ denominator

Im having some trouble converting type str to numbers. I use a separate text-file containing the following numbers 1, 2, 3, 4, 5, 6 and then I import these numbers into python and save them as a list. …

discord.py MemberNotFound exception when passing a real member

When I pass *grant @user add in Discord I get the following exception: Ignoring exception in command grant: Traceback (most recent call last):File "/Users/test/PycharmProjects/slapdash/venv/lib/py…

using argument end= for print function in Python 3.3

Consider the following code:i=0 while i<5:print(i, end=" ")i = i + 1which results in the output:0 1 2 3 4if i want to add a string "the result" before 1 2 3 4, output expected: t…

Get the max value on a list of tuples

I have a list of tuples:card_list= [(2, (1, S)), (0, (12, H)), (1, (5, C)]This list contains cards: (cardindex, (value, suit)) where cardindex is a index to store the position of the card but irrelevan…