社区所有版块导航
Python
python开源   Django   Python   DjangoApp   pycharm  
DATA
docker   Elasticsearch  
aigc
aigc   chatgpt  
WEB开发
linux   MongoDB   Redis   DATABASE   NGINX   其他Web框架   web工具   zookeeper   tornado   NoSql   Bootstrap   js   peewee   Git   bottle   IE   MQ   Jquery  
机器学习
机器学习算法  
Python88.com
反馈   公告   社区推广  
产品
短视频  
印度
印度  
私信  •  关注

Ralph Macêdo

Ralph Macêdo 最近创建的主题
Ralph Macêdo 最近回复了
3 年前
回复了 Ralph Macêdo 创建的主题 » Python——从列表中获取最大年龄。文件

我相信有一个内置功能可以解析像您发布的那样的复合字符串,但我不知道,我已经创建了一个CustomParse类来完成这项工作:

class CustomParser():
    def __init__(self, line: str, delimiter: str):
        self.line = line
        self.delimiter = delimiter

    def split(self):
        word = ''
        words = []
        inside_string = False

        for letter in line:
            if letter in '“”"':
                inside_string = not inside_string
                continue
            if letter == self.delimiter and not inside_string:
                words.append(word.strip())
                word = ''
                continue
            word += letter
        words.append(word.strip())

        return words


with open('people_data.csv') as file:
    ages = []
    for line in file:
        ages.append(CustomParser(line, ',').split()[2])
    print(max(ages[1:]))

希望有帮助。

3 年前
回复了 Ralph Macêdo 创建的主题 » Python if and else语句不是预期的输出

我认为 elif 贝娄应该评估<=2,而不是1:

        elif attempt <= 2:

但最后一条“再试一次”的信息仍然打印出来。您可以在“重试”消息之前设置一个尝试检查条件来解决这个问题。如果条件求值为True,则中断循环:

        elif attempt <= 2:
            print('HAAAAAAAAAAAAAAAAAAAAAAAAAAAA IMAGINE NOT GETTING THAT RIGHT!!!')
            print('L bozo + ratio')
            attempt = attempt + 1
            if attempt > 2:
                break            
            guess = input('Try again ')

在这种情况下,请记住调整您的状态,因为不再需要尝试检查。

如果可以的话,我在代码中做了一些重构,因此您可以查看其他方法来实现相同的目标。

def check_guess(guess, answer, attempts):
    global score
    global name
    while True:
        if guess == answer:
            print('Correct wow, i expected worse from someone named %s' % name)
            score = [0, 1, 2, 3]
            final_score = score[attempts]
            print(f'Score: {final_score}')
            break

        attempts -= 1

        if attempts == 0:
            print('the correct answer was %s' % answer)
            print('There is always next time!!')
            break
        
        print('HAAAAAAAAAAAAAAAAAAAAAAAAAAAA IMAGINE NOT GETTING THAT RIGHT!!!')
        print('L bozo + ratio')
        guess = input('Try again ')

score = 0
print('Welcome to the animal quiz')
print()
print('In this game you will have to guess the animal')

name = input('What is your name? ')
print('Cool name, I feel like i heard of it before hmmm %s..' % name)
print()
print('Enough stalling onto the game!!!')

guess1 = input('Which bear lives in the north pole ')
check_guess(guess1.strip().lower(), 'polar bear', attempts=3)