V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
推荐学习书目
Learn Python the Hard Way
Python Sites
PyPI - Python Package Index
http://diveintopython.org/toc/index.html
Pocoo
值得关注的项目
PyPy
Celery
Jinja2
Read the Docs
gevent
pyenv
virtualenv
Stackless Python
Beautiful Soup
结巴中文分词
Green Unicorn
Sentry
Shovel
Pyflakes
pytest
Python 编程
pep8 Checker
Styles
PEP 8
Google Python Style Guide
Code Style from The Hitchhiker's Guide
gypsy
V2EX  ›  Python

大佬们,一个关于 list 的作业不会做,求指导

  •  
  •   gypsy · 2020-10-20 20:48:17 +08:00 · 1227 次点击
    这是一个创建于 1281 天前的主题,其中的信息可能已经有所发展或是发生改变。
    就是把用户输入的 list 里的数字转换成 x 数量的星号。目前卡在两个地方
    1.input ()命令可以输入 list 吗?
    2. 可以直接用加减乘除来打印出相对应数量的星号吗?
    感谢

    Problem 2:
    Write a function that takes a list of numbers as parameters. Then print a histogram based on those numbers. Use asterisks to draw the histogram.
    For example, given the list [3, 7, 9, 5]. The function should produce the following:

    ***
    *******
    *********
    *****

    Call the function with the list [3, 7, 9, 5].
    Then call the function with the list [2, 0, 1].

    Hint: List was covered in the A3 notebook. We will formally discuss list in A5 notebook.
    You might want to first write the histogram code without a function. Then after code is working, write the code with a function. Please submit only the code with a function.
    learningman
        1
    learningman  
       2020-10-20 20:55:00 +08:00
    代写作业,50 一位
    qefrgty
        2
    qefrgty  
       2020-10-20 21:00:29 +08:00 via Android
    先想办法取 list 的元素,

    然后把这个元素赋值给一个变量,

    然后打印变量个星号。

    emmmm…这是最基本的思路了吧…
    user8341
        3
    user8341  
       2020-10-20 21:01:11 +08:00
    lambdafate
        4
    lambdafate  
       2020-10-20 21:04:53 +08:00
    1. "Write a function that takes a list of numbers as parameters. ". 不需要 input 吧,list 作为函数参数传进来
    2. 可以这样 >>> "*" * n # n 是一个整数
    gypsy
        5
    gypsy  
    OP
       2020-10-20 21:08:29 +08:00
    @learningman
    @qefrgty
    @user8341
    @lambdafate

    做完了,输出是对的,感谢。

    import ast
    list=ast.literal_eval(input())

    def star(a):
    return "*" * a

    for i in list:
    print(star(i))
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   1013 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 26ms · UTC 20:45 · PVG 04:45 · LAX 13:45 · JFK 16:45
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.