用于作业的高级python脚本

时间:2019-04-26 06:10:28

标签: python python-3.x

有人可以帮助我理解此代码吗? 我的朋友将其发送给我,但是不想帮助我理解它,这是我的家庭作业。 如果您为每行代码写下一个主题标签以帮助我理解它,将不胜感激。

(function(){
    /*
    Define all colors used.
  */
    var colorSet = new am4core.ColorSet();
    colorSet.list = ["#18dc08", "#e2ed0c", "#3be791", "#0eade6", "#8f55e9", "#dd0a4b"].map(function(color) {
        return new am4core.color(color);
  });

  var size = 16.6;

  /*
    Creates a pie slice
  */
  function createPie(chart, targetAttribute, positionRadiusInPie){
    var pieSeries = chart.series.push(new am4charts.PieSeries());
    pieSeries.dataFields.value = "size"; // the percentage size so all slice are equals
    pieSeries.dataFields.category = "name";
    pieSeries.slices.template.strokeWidth = 1;
    pieSeries.slices.template.strokeOpacity = 1;
    pieSeries.alignLabels = false;
    pieSeries.ticks.template.disabled = true;
    // Enable label
    pieSeries.labels.template.text = "{"+ targetAttribute +".length}";
    pieSeries.labels.template.radius = am4core.percent(positionRadiusInPie); // magic number
    // color
    pieSeries.colors = colorSet;
    pieSeries.labels.template.fill = am4core.color("white");
    pieSeries.slices.template.stroke = am4core.color("#fff");
    // Disable sliding out of slices
    //pieSeries.slices.template.states.getKey("hover").properties.shiftRadius = 0;
    //pieSeries.slices.template.states.getKey("hover").properties.scale = 1.1;
    // Tooltip conf
    // https://www.amcharts.com/docs/v4/reference/pieseries/#Events
    pieSeries.slices.template.events.on("over", function(ev){
        // https://www.amcharts.com/docs/v4/tutorials/tooltips-with-rich-html-content/
        var modalContent = [
        '<center><strong>'+ev.target.dataItem.dataContext.name+'</strong></center>',
        '<center><i>'+targetAttribute+'</i></center>',
                '<hr />',
      ].join("");

        if(!ev.target.dataItem.dataContext[targetAttribute] || 
            ev.target.dataItem.dataContext[targetAttribute].length == 0){
        modalContent += [
            '<span>No information</span>'
        ].join("");
      }else{
        modalContent += [
            '<table>',
            '<tbody>'
        ].join("");

        for(var i = 0; i < ev.target.dataItem.dataContext[targetAttribute].length; i++){
            var item = ev.target.dataItem.dataContext[targetAttribute][i];
          modalContent += [
                '<tr>',
                '<th align="left">Name</th>',
                '<td align="right">'+item.name+'</td>',
              '</tr>',
          ].join("");
        }

        modalContent += [
              '</tbody>',
            '</table>',
          '<hr />'
        ].join("");
      }
        pieSeries.slices.template.tooltipHTML = modalContent;
    }, this);

    // https://www.amcharts.com/docs/v4/reference/columnseries/#Events
    pieSeries.slices.template.events.on("inited", function(ev) {
      if(!ev.target.dataItem.dataContext[targetAttribute] || ev.target.dataItem.dataContext[targetAttribute].length == 0){
        ev.target.fillOpacity = 0.5;
      }
    }, this);
  }

  // Themes begin
  am4core.useTheme(am4themes_animated);
  // Themes end

  // Create chart instance
  var chart = am4core.create("chartdiv", am4charts.PieChart);

  // Let's cut a hole in our Pie chart the size of 20% the radius
  chart.innerRadius = am4core.percent(20);
  chart.pullOutRadius = 0;

  // Add data: can be fetched from a webservice
  chart.data = [
  {
    "name": "Sector1",
    "color": colorSet.list[0].hex,
    "size": size,
    "category1": [
      {
        name: "test 1"
      },
      {
        name: "titi"
      }
    ],
    "category2": [
      //{
  //        name: "test 1"
  //    }
    ],
    "category3" : [
      //{
      //    name: "test 1"
      //}
    ],
    "category4" : [
      {
        name: "test 1"
      }
    ]
  }, 
  {
    "name": "Sector2",
    "color": colorSet.list[1].hex,
    "size": size,
    "category1":  [
      {
        name: "test 1"
      }
    ],
    "category2": [
      {
        name: "test 1"
      }
    ],
    "category3" : [
      {
        name: "test 1"
      }
    ],
    "category4" : [
      {
        name: "test 1"
      }
    ]
  },
  {
    "name": "Sector3",
    "color": colorSet.list[2].hex,
    "size": size,
   "category1":  [
      {
        name: "test 1"
      }
    ],
    "category2": [
      //{
      //    name: "test 1"
      //}
    ],
    "category3" : [
      {
        name: "test 1"
      }
    ],
    "category4" : [
      {
        name: "test 1"
      }
    ]
  }, 
  {
    "name": "Sector4",
    "color": colorSet.list[3].hex,
    "size": size,
    "category1":  [
      {
        name: "test 1"
      }
    ],
    "category2": [
      {
        name: "test 1"
      }
    ],
    "category3" : [
      {
        name: "test 1"
      }
    ],
    "category4" : [
      {
        name: "test 1"
      }
    ]
  }, 
  {
    "name": "Sector5",
    "color": colorSet.list[4].hex,
    "size": size,
     "category1":  [
      {
        name: "test 1"
      }
    ],
    "category2": [
      {
        name: "test 1"
      }
    ],
    "category3" : [
      {
        name: "test 1"
      }
    ],
    "category4" : [
      {
        name: "test 1"
      }
    ]
  }, 
  {
    "name": "Sector6",
    "color": colorSet.list[5].hex,
    "size": size,
    "category2": [
      {
        name: "test 1"
      }
    ],
    "category1": [
      {
        name: "test 1"
      }
    ],

    "category3" : [
      {
        name: "test 1"
      }
    ],
    "category4" : [
      {
        name: "test 1"
      }
    ]
  }
  ];

  /*
    category1
  */
  createPie(chart, "category1", -28);

  /*
    category2
  */
  createPie(chart, "category2", -20);

  /*
    category3
  */
  createPie(chart, "category3", -10);

  /*
    Business ops
  */
 createPie(chart, "category4", -8);

  /*
    Legend builder
    // https://www.amcharts.com/docs/v4/reference/ipiechartevents/
  */
  chart.events.on('inited', function(event) {
  // populate our custom legend when chart renders
    chart.customLegend = document.getElementById('legend');
    // https://www.amcharts.com/docs/v4/tutorials/truncating-legend-labels/
    //chart.customLegend.labels.template.truncate = true;
    for (var i in chart.data) {
      var row = chart.data[i];
      var color = chart.data[i].color;
      var name = row.name;
      chart.customLegend.innerHTML += 
      [
        '<div class="legend-item" id="legend-item-' + i + '" style="color: ' + color + ';">',
          '<div class="legend-marker" style="background: ' + color + '"></div>',
          name,
        '</div>'
      ].join("");
    }
  });

})()

1 个答案:

答案 0 :(得分:1)

passlen是一个字符串,您正尝试将其转换为整数,您不应这样做,而应使用len来获取输入字符串的长度

根据密码中的字符数,更正下面的代码,它从字符列表中随机选择许多字符,并打印出字符串

from random import *

#List of characters to choose from
s="""1234567890+!#$%&/()=?qwertyuiopasdfghjklzxcvbnmQWERTYUIOPASDFGHJKLZXCVBNM
,.-;:_*@<>≤≥©™£€∞§|[]≈±`•Ω醵üıπß∂ƒ¸˛√ª÷≈ç‹›‘’°˝É‡˜ÜŒ∏◊∑∆∫¯˘¬º⁄Ç«»“”"""

while True:
    while True:

        #Take the password string as input, and use len to get the length of password
        passlen = len(input("""Password must between 8 and 16 character   """))

        #Once the password of correct length is printed, exit the loop
        if 8 <= passlen <= 16:
            break

    #For the length of password, pick a random character from string s and print it
    for i in range(passlen):
        print(s[randint(0, len(s))], end="")
    print()

输出看起来像

Password must between 8 and 16 character   abcdefgh
«9¯
’r|)
Password must between 8 and 16 character   abcdefghij
d2◊∫r!lT¯ı
Password must between 8 and 16 character   abcd
Password must between 8 and 16 character   abcdefghijkl
ÉtBWmeFVZr_÷