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

2020-10-20 20:48:17 +08:00
 gypsy
就是把用户输入的 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.
1238 次点击
所在节点    Python
5 条回复
learningman
2020-10-20 20:55:00 +08:00
代写作业,50 一位
qefrgty
2020-10-20 21:00:29 +08:00
先想办法取 list 的元素,

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

然后打印变量个星号。

emmmm…这是最基本的思路了吧…
user8341
2020-10-20 21:01:11 +08:00
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
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))

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://www.v2ex.com/t/716852

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX