简单的Python函数为什么我得到这么多错误?

2024-04-25 12:34:30 发布

您现在位置:Python中文网/ 问答频道 /正文

import random

def a():
    str1 = '''Do people need to compare themselves with others in order to appreciate what they have?
        Are widely held views often wrong, or are such views more likely to be correct?
        Is there any value for people to belong only to a group or groups with which they have something in common?
        Is it always best to determine one's own views of right and wrong, or can we benefit from following the crowd?
        Is it more valuable for people to fit in than to be unique and different?
        Are people more likely to be productive and successful when they ignore the opinions of others?
        Should we pay more attention to people who are older and more experienced than we are?
        Should society limit people's exposure to some kinds of information or forms of expression?
        Can a group of people function effectively without someone being in charge?
        Is it important to question the ideas and decisions of people in positions of authority?
        Should society limit people's exposure to some kinds of information or forms of expression?
        Is education primarily the result of influences other than school?
        Should schools help students understand moral choices and social issues?
        Is it always better to be original than to imitate or use the ideas of others?
        Is it better for a society when people act as individuals rather than copying the ideas and opinions of others?
        Is creativity needed more than ever in the world today?
        Can people ever be truly original?
        Do we put too much value on the ideas or actions of individual people?
        Does planning interfere with creativity?
        Do people truly benefit from hardship and misfortune?
        Do we really benefit from every event or experience in some way?
        Do people place too much emphasis on winning?
        Do people learn more from losing than from winning?
        Does true learning only occur when we experience difficulties?
        Does being ethical make it hard to be successful?
        Can knowledge be a burden rather than a benefit?
        Is persistence more important than ability in determining a person's success?
        Is the effort involved in pursuing any goal valuable, even if the goal is not reached
        ?Is identity something people are born with or given, or is it something people create for themselves?
        Is it best for people to accept who they are and what they have, or should people always strive to better themselves?
        Do success and happiness depend on the choices people make rather than on factors beyond their control?
        Are people more likely to be happy if they focus on goals other than their own happiness?
        Is it more important to do work that one finds fulfilling or work that pays well?
        Do highly accomplished people achieve more than others mainly because they expect more of themselves?
        Can people achieve success only if they aim to be perfect?
        Is it best to have low expectations and to set goals we are sure of achieving?
        Is it necessary for people to combine their efforts with those of others in order to be most effective?
        Are organizations or groups most successful when their members pursue individual wishes and goals?
        Do people achieve more success by cooperation than by competition?
        Does fame bring happiness, or are people who are not famous more likely to be happy?
        Are people's actions motivated primarily by a desire for power over others?
        Do people achieve greatness only by finding out what they are especially good at and developing that attribute above all else?
        Are all important discoveries the result of focusing on one subject?
        Does a strong commitment to technological progress cause a society to neglect other values, such as education and the protection of the environment?
        Are there benefits to be gained from avoiding the use of modern technology, even when using it would make life easier?
        Has today's abundance of information only made it more difficult for us to understand the world around us?
        Is the most important purpose of technology today different from what it was in the past?
        Have modern advancements truly improved the quality of people's lives?
        Do newspapers, magazines, television, radio, movies, the Internet, and other media determine what is important to most people?
        Should modern society be criticized for being materialistic?
        Do we benefit from learning about the flaws of people we admire and respect?
        Should we limit our use of the term "courage" to acts in which people risk their own well-being for the sake of others or to uphold a value?
        Should we admire heroes but not celebrities?
        Is there a value in celebrating certain individuals as heroes?
        Do all established traditions deserve to remain in existence?
        Do people need to "unlearn," or reject, many of their assumptions and ideas?
        Should people always prefer new things, ideas, or values to those of the past?
        Do incidents from the past continue to influence the present?
        Do memories hinder or help people in their effort to learn from the past and succeed in the present?
        Is it always necessary to find new solutions to problems?
        Should people always be loyal?
        Do circumstances determine whether or not we should tell the truth?
        Can deception—pretending that something is true when it is not—sometimes have good results?
        Is it sometimes necessary to be impolite?
        Is acting an essential part of everyday life?
        Is compromise always the best way to resolve a conflict?
        Should people choose one of two opposing sides of an issue, or is the truth usually found "in the middle"?
        Is the main value of the arts to teach us about the world around us?
        Can books and stories about characters and events that are not real teach us anything useful?
        Can common sense be trusted and accepted, or should it be questioned?
        Do people put too much emphasis on learning practical skills?
        Should people take more responsibility for solving problems that affect their communities or the nation in general?
        Should people let their feelings guide them when they make important decisions?
        Can people have too much enthusiasm?
        Do images and impressions have too much of an effect on people?
        Are decisions made quickly just as good as decisions made slowly and carefully?
        Should people change their decisions when circumstances change, or is it best for them to stick with their original decisions?
        Is it better to change one's attitude than to change one's circumstances?
        Is criticism—judging or finding fault with the ideas and actions of others—essential for personal well-being and social progress?
        Does having a large number of options to choose from make people happy?'''.replace("'", "")
    list1 = str1.split('        ')
    return list[random.randomint(list1[0],list1[len(list1)])]

我正在尝试创建一个python程序,它会随机给我一个文章提示


Tags: orandofthetoinforis
2条回答

将其改为使用random.choice:另外,您可能希望将标题存储在文本文件中,或者以某种方式对它们进行pickle。。。在

或者把它们当作:

titles = [
    'lakjdflasjkfs?',
    'adfasdfssf?',
    'afadff?'
]

样本代码:

^{pr2}$

也许,您可能希望这样做,而不是splitlines()

titles = [line.strip() + '?' for line in str1.split('?')]

但如何分割它取决于你-格式化它更好的开始!在

运行脚本时我遇到的错误是:

File "script.py", line 66
SyntaxError: Non-ASCII character '\xe2' in file python.py on line 67, but no encoding declared; see http://www.python.org/peps/pep-0263.html for details

这意味着您的脚本中有非ASCII字符。我无法从视觉上看到它们,所以我写了一个脚本来帮我:

^{pr2}$

输出:

bad character: line: 65 col: 21 (�) [ e2 ]
bad character: line: 65 col: 23 (�) [ 94 ]
bad character: line: 65 col: 72 (�) [ e2 ]
bad character: line: 65 col: 74 (�) [ 94 ]
bad character: line: 81 col: 20 (�) [ e2 ]
bad character: line: 81 col: 22 (�) [ 94 ]
bad character: line: 81 col: 84 (�) [ e2 ]
bad character: line: 81 col: 86 (�) [ 94 ]

看起来您使用的是非ASCII连字符。用普通连字符替换它们来解决第一个问题。或者,看看如何将文件的编码指定给Python解释器。在

第二个问题是您没有正确地使用random模块。下面是固定的脚本:

import random

def a():
    str1 = '''Do people need to compare themselves with others in order to appreciate what they have?
        Are widely held views often wrong, or are such views more likely to be correct?
        Is there any value for people to belong only to a group or groups with which they have something in common?
        Is it always best to determine one's own views of right and wrong, or can we benefit from following the crowd?
        Is it more valuable for people to fit in than to be unique and different?
        Are people more likely to be productive and successful when they ignore the opinions of others?
        Should we pay more attention to people who are older and more experienced than we are?
        Should society limit people's exposure to some kinds of information or forms of expression?
        Can a group of people function effectively without someone being in charge?
        Is it important to question the ideas and decisions of people in positions of authority?
        Should society limit people's exposure to some kinds of information or forms of expression?
        Is education primarily the result of influences other than school?
        Should schools help students understand moral choices and social issues?
        Is it always better to be original than to imitate or use the ideas of others?
        Is it better for a society when people act as individuals rather than copying the ideas and opinions of others?
        Is creativity needed more than ever in the world today?
        Can people ever be truly original?
        Do we put too much value on the ideas or actions of individual people?
        Does planning interfere with creativity?
        Do people truly benefit from hardship and misfortune?
        Do we really benefit from every event or experience in some way?
        Do people place too much emphasis on winning?
        Do people learn more from losing than from winning?
        Does true learning only occur when we experience difficulties?
        Does being ethical make it hard to be successful?
        Can knowledge be a burden rather than a benefit?
        Is persistence more important than ability in determining a person's success?
        Is the effort involved in pursuing any goal valuable, even if the goal is not reached
        ?Is identity something people are born with or given, or is it something people create for themselves?
        Is it best for people to accept who they are and what they have, or should people always strive to better themselves?
        Do success and happiness depend on the choices people make rather than on factors beyond their control?
        Are people more likely to be happy if they focus on goals other than their own happiness?
        Is it more important to do work that one finds fulfilling or work that pays well?
        Do highly accomplished people achieve more than others mainly because they expect more of themselves?
        Can people achieve success only if they aim to be perfect?
        Is it best to have low expectations and to set goals we are sure of achieving?
        Is it necessary for people to combine their efforts with those of others in order to be most effective?
        Are organizations or groups most successful when their members pursue individual wishes and goals?
        Do people achieve more success by cooperation than by competition?
        Does fame bring happiness, or are people who are not famous more likely to be happy?
        Are people's actions motivated primarily by a desire for power over others?
        Do people achieve greatness only by finding out what they are especially good at and developing that attribute above all else?
        Are all important discoveries the result of focusing on one subject?
        Does a strong commitment to technological progress cause a society to neglect other values, such as education and the protection of the environment?
        Are there benefits to be gained from avoiding the use of modern technology, even when using it would make life easier?
        Has today's abundance of information only made it more difficult for us to understand the world around us?
        Is the most important purpose of technology today different from what it was in the past?
        Have modern advancements truly improved the quality of people's lives?
        Do newspapers, magazines, television, radio, movies, the Internet, and other media determine what is important to most people?
        Should modern society be criticized for being materialistic?
        Do we benefit from learning about the flaws of people we admire and respect?
        Should we limit our use of the term "courage" to acts in which people risk their own well-being for the sake of others or to uphold a value?
        Should we admire heroes but not celebrities?
        Is there a value in celebrating certain individuals as heroes?
        Do all established traditions deserve to remain in existence?
        Do people need to "unlearn," or reject, many of their assumptions and ideas?
        Should people always prefer new things, ideas, or values to those of the past?
        Do incidents from the past continue to influence the present?
        Do memories hinder or help people in their effort to learn from the past and succeed in the present?
        Is it always necessary to find new solutions to problems?
        Should people always be loyal?
        Do circumstances determine whether or not we should tell the truth?
        Can deception-pretending that something is true when it is not-sometimes have good results?
        Is it sometimes necessary to be impolite?
        Is acting an essential part of everyday life?
        Is compromise always the best way to resolve a conflict?
        Should people choose one of two opposing sides of an issue, or is the truth usually found "in the middle"?
        Is the main value of the arts to teach us about the world around us?
        Can books and stories about characters and events that are not real teach us anything useful?
        Can common sense be trusted and accepted, or should it be questioned?
        Do people put too much emphasis on learning practical skills?
        Should people take more responsibility for solving problems that affect their communities or the nation in general?
        Should people let their feelings guide them when they make important decisions?
        Can people have too much enthusiasm?
        Do images and impressions have too much of an effect on people?
        Are decisions made quickly just as good as decisions made slowly and carefully?
        Should people change their decisions when circumstances change, or is it best for them to stick with their original decisions?
        Is it better to change one's attitude than to change one's circumstances?
        Is criticism-judging or finding fault with the ideas and actions of others-essential for personal well-being and social progress?
        Does having a large number of options to choose from make people happy?'''.replace("'", "")
    list1 = str1.split('        ')
    return random.choice(list1)

if __name__ == "__main__":
    print a()

相关问题 更多 >