相关疑难解决方法(0)

SyntaxError:使用匹配大小写时语法无效

我一直在尝试使用匹配大小写而不是一百万个 IF 语句,但我尝试的任何操作都会返回错误:

    match http_code:
          ^
SyntaxError: invalid syntax
Run Code Online (Sandbox Code Playgroud)

我还尝试过测试我发现的示例,这些示例也会返回此错误,包括以下错误:

http_code = "418"

match http_code:
    case "200":
        print("OK")

    case "404":
        print("Not Found")

    case "418":
        print("I'm a teapot")

    case _:
        print("Code not found")
Run Code Online (Sandbox Code Playgroud)

我知道匹配案例对于 python 来说是相当新的,但我使用的是 3.10,所以我不确定为什么它们总是返回这个错误。

python pattern-matching

43
推荐指数
1
解决办法
11万
查看次数

IndentationError:意外的unindent为什么?

IndentationError:意外的unindent为什么???

#!/usr/bin/python
import sys
class Seq:
    def __init__(self, id, adnseq, colen):
        self.id     = id
        self.dna    = adnseq
        self.cdnlen = colen
        self.prot   = ""
    def __str__(self):
        return ">%s\n%s\n" % (self.id, self.prot)
    def translate(self, transtable):
        self.prot = ""
        for i in range(0,len(self.dna),self.cdnlen):
            codon = self.dna[i:i+self.cdnlen]
            aa    = transtable[codon]
            self.prot += aa
    def parseCommandOptions(cmdargs):
        tfname = cmdargs[1]
        sfname = cmdargs[2]
        return (tfname, sfname)
    def readTTable(fname):
        try:
            ttable = {}
            cdnlen = -1
            tfile = open(fname, "r")
            for line in tfile:
                linearr = line.split() …
Run Code Online (Sandbox Code Playgroud)

python indentation

23
推荐指数
2
解决办法
7万
查看次数

if语句中冒号的语法错误

我是python的新手,并且正在制作一种游戏,作为我猜测1到10之间的数字的第一个项目之一,然后用户猜对了.他们有三个猜测,该程序告诉用户他们是否需要在下一次猜测时更高或更低.带错误的代码部分并不重要,因为如果用户输入两次相同的答案,只允许他们重新猜测第一次但不允许重新获取第二.在代码上,我已经标出了问题所在.就像我说的,我是python的新手,这可能是一些业余的noobie错误.提前致谢.

import time # This imports the time module.
import random # This imports the random module.

MyNumber = random.randrange(1,10) # This picks a number for the variable 'MyNumber'

firstGuess = int(input('Ok then, we shall begin! What is your first guess?'))
print()
if firstGuess == (MyNumber):
 print('Well done! You win!')
 time.sleep(3)
 exit()
if firstGuess < MyNumber:
 print('Go Higher!')
 time.sleep(1)
if firstGuess > MyNumber:
 print('Go Lower!')
 time.sleep(1)

print()
secondGuess = int(input('Better luck this time! What is your second guess?'))
print()
if …
Run Code Online (Sandbox Code Playgroud)

python python-3.x

16
推荐指数
1
解决办法
4万
查看次数

语法错误:语法无效?

下午好,

我正在用 python 开发一个脚本,当我尝试从终止符/终端编译它时,我总是收到此错误,但我无法理解语法错误在哪里?

文件“_case1.py”,第 128 行

print ('########################')
    ^
Run Code Online (Sandbox Code Playgroud)

语法错误:语法无效

然后我只是改变句子的位置,我得到的是..

print '########################'
    ^
Run Code Online (Sandbox Code Playgroud)

语法错误:语法无效

问题是什么?

python syntax

6
推荐指数
1
解决办法
3万
查看次数

Python 3.5 中的 F 字符串无效语法

我知道那F Strings是在Python 3.6. 为此,我收到了错误 -Invalid Syntax

DATA_FILENAME = 'data.json'
def load_data(apps, schema_editor):
    Shop = apps.get_model('shops', 'Shop')
    jsonfile = Path(__file__).parents[2] / DATA_FILENAME

    with open(str(jsonfile)) as datafile:
        objects = json.load(datafile)
        for obj in objects['elements']:
            try:
                objType = obj['type']
                if objType == 'node':
                    tags = obj['tags']
                    name = tags.get('name','no-name')
                    longitude = obj.get('lon', 0)
                    latitude = obj.get('lat', 0)
                    location = fromstr(F'POINT({longitude} {latitude})', srid=4326)
                    Shop(name=name, location = location).save()
            except KeyError:
                pass    
Run Code Online (Sandbox Code Playgroud)

错误 -

location = (F'POINT({longitude} {latitude})', srid=4326)
                                           ^
SyntaxError: …
Run Code Online (Sandbox Code Playgroud)

python python-3.x

6
推荐指数
1
解决办法
1万
查看次数

Python:如何解决IndentationError

我的IndentationError看起来似乎无法解决. http://pastebin.com/AFdnYcRc.

#!/usr/bin/env python
import os
import glob
import shutil
import mutagen
from sys import exit

musicdir = raw_input("What directory are the music files located in? : ")
musfile = glob.glob(musicdir + '/' + "*.mp3")
musfile1 = glob.glob(musicdir + '/' + "*.flac")
musfile.extend(musfile1)
newmusicdir = raw_input("What directory should the music files be organized into? : ")


done = False

while not done:
    for m in musfile:
        if musfile:
            try:
                musta = mutagen.File(m, easy=True)
                mar = str(musta['artist'][0])
                mal = str(musta['album'][0]) …
Run Code Online (Sandbox Code Playgroud)

python indentation

3
推荐指数
3
解决办法
1335
查看次数

if语句的语法无效

我将粘贴整个函数,因为它不长:

def decideTile():
    global tile
    global exp
    global maxexp

    tile += 1
    exp += math.ceil(random.randrange(math.ceil((maxexp/2)/2,maxexp/2))

    if exp >= maxexp:
       levelUp()
    else:
       tileChoices = ['Battle','Loot','Nothing']
       fileType = random.choice(tileChoices)

    if tileType == 'Battle':
        battle()
    elif tileType == 'Loot':
        loot()
    elif tileType == 'Nothing':
        nothing()
Run Code Online (Sandbox Code Playgroud)

现在,Python正在说

if exp >= maxexp:
Run Code Online (Sandbox Code Playgroud)

它的一部分是'无效语法',我不完全确定原因.感谢帮助!

python syntax if-statement python-3.x

2
推荐指数
1
解决办法
2311
查看次数

多个if语句后语法无效

knowledge = input().lower()

if knowledge in list1:
    m = float(input())

if knowledge in list2:
    g = float(input())

if knowledge in list3:
    Fz = float(input())

if knowledge in list4:
    W = float(input()

if knowledge in list5:
    F = float(input()

if knowledge in list6:
    ?x = float(input()
Run Code Online (Sandbox Code Playgroud)

列表1到6都在代码的另一部分中定义,例如:list1 ="m","mass","kg"

从列表4的if语句(包括列表4中的if语句和其他语句)中,我得到一个无效的语法,它指向列表4的if语句末尾的":".

当删除列表4的if语句时,它将指向列表5的":",依此类推.

这里发生了什么,我该如何解决?这是我的大量if语句的原因吗?

python if-statement python-3.x

0
推荐指数
1
解决办法
305
查看次数

无效的语法指向python中的“ return”

我只是在探索的广泛语言python。我在很大程度上依靠内置的错误检测和谷歌搜索来调试程序。这次我没有运气。感谢您的时间!

我正进入(状态

“第5行的语法无效”

这是我的简单的python代码HANGMAN-game

import random

def __secretword__():
    secret_word = word_list[random.randomint(len(word_list))
    return secret_word #THIS IS LINE 5

def __ask__():
    ans = input("Would you like to play more? (yes/no): ")
    if ans == "yes"
        __secretword__()
    else:
        print(":(")


__secretword__()

word_list = ["nei", "brun", "ja", "smile", "glad", "gal"]
secret_word = secret_word
sw_list = list(secret_word)
guess_lines = ["_"] * len(secret_word)
mistakes = []
lives = 2 * len(secret_word)

print(guess_lines)

user_guess = input("Guess a letter: ")

while(lives != 0)   
    if user_guess …
Run Code Online (Sandbox Code Playgroud)

python syntax python-3.x

0
推荐指数
1
解决办法
7782
查看次数

if语句中的Python语法错误

我坚持使用Python if语句.我完成了所有的一切,但在运行我的progarmm时,它在第一行给出了我的语法错误:我很确定我做的一切都正确但是因为我对Python和编程很新,它可能只是一个这是一个非常愚蠢的错误...感谢帮助你们!

if a == 2:   
StartDeckNeighbourright = StartDeck[a + 1]
StartDeckNeighbourright2 = StartDeck[a + 2]
Run Code Online (Sandbox Code Playgroud)

python syntax syntax-error

-1
推荐指数
1
解决办法
2万
查看次数