syntax

Tricks and tips for beginners to shorten and simplify code

Tricks and tips for beginners to shorten and simplify code Question: I am looking for ways to shorten and simplify my python code. Here is one example of one small rock/paper/scissors game I wrote earlier. The code kinda looks too long to me and I want to try to learn how to shorten my code. …

Total answers: 1

How can I fix this cod?

How can I fix this cod? Question: x = str(input("type a letter : ")) if x == x.islower: print ("capital") else: print("small") This is my code I wanna tell a letter is capital or small but Idk what’s the problem Asked By: Mehdi || Source Answers: There are some basic logical and syntactic mistakes in …

Total answers: 2

Why is this throwing an invalid syntax error?

Why is this throwing an invalid syntax error? Question: I have been trying to solve 3sum on leetcode. The line ‘lower +=1’ (below the first triplets.append) is throwing an invalid syntax error but I have no idea why. class Solution(object): def threeSum(self, nums): if len(nums) < 3: return([]) triplets = [] nums = sorted(nums) for …

Total answers: 2

How can I debug this Python syntax error with f string

How can I debug this Python syntax error with f string Question: def write_users_group(heading_writer, heading, user_writer, users): heading_writer.writerow([f"{heading}", f"{len(users)} users"]) user_writer.writeheader() for user in users: user_writer.writerow(user) heading_writer.writerow([" "]) Error: File "/Users/ashirwadniv/Downloads/gitlab/users.py", line 59 heading_writer.writerow([f"{heading}", f"{len(users)} users"]) ^ SyntaxError: invalid syntax Asked By: Ashirwad.nivalkar || Source Answers: This syntax is only allowed for Python version 3.6 …

Total answers: 1

'for statement' without a colon

'for statement' without a colon Question: test_keys = ["Rash", "Kil", "Varsha"] test_values = [1, 4, 5] # using dictionary comprehension # to convert lists to dictionary res = {test_keys[i]: test_values[i] for i in range(len(test_keys))} # Printing resultant dictionary print ("Resultant dictionary is : " + str(res)) above, there should be an ending colon " : …

Total answers: 3

Python Visual Studio code import Syntax problems // color

Python Visual Studio code import Syntax problems // color Question: For some reasons my Visual Studio code doesn´t show the color for the imported library. In the attached file you see what I mean. I tried already: Deinstalled vsc and reinstalled it again deleted the settings.json in (%APPDATA%CodeUser) deinstalled all extensions and reinstalled it again …

Total answers: 2

Python Selenium: Selecting from a list by text

Python Selenium: Selecting from a list by text Question: I have spent the past few days trying to solve this problem. I store a string in a variable and i am trying to automate using selenium to select the item whose text()=’string stored in the variable’. I noticed i could not select some of the …

Total answers: 2

Function: Finding Col names from a DataFrame

Function: Finding Col names from a DataFrame Question: This is the error I am getting: SyntaxError: invalid syntax I am attempting to pass the variable dataname to the function Colnames so that it will return the names of the columns for the respective dataframe. I want to display an error message on invalid user input …

Total answers: 2

Why I need to add an extra parenthesis for sum() function?

Why I need to add an extra parenthesis for sum() function? Question: if sum() is a function in python, why does it need an extra parenthesis to work? Like other functions why sum() is not working with single parenthesis? It’s not working when it’s like this: num1 = int(input()) num2 = int(input()) total = sum(num1, …

Total answers: 3