i've made this little thing and i need the output to be, for example, like this:
****
*******
**
****
But i get the output this way:
************
Could you help me? Here's the program.
import math
def MakingGraphic(number):
list = [number]
graphic = number * '*'
return(graphic)
list = 0
howmany = int(input("How many numbers will you write?"))
for i in range(0, howmany, 1):
number = int(input("Write a number "))
list = list + number
result = MakingGraphic(list)
print(result)
add a "\\n" to return to next line. eg result = MakingGraphic(list) + "\\n"
Why do you use a list by the way ?
import math
def MakingGraphic(number):
return number * '*'
result = ''
howmany = int(input("How many numbers will you write?"))
for i in range(0, howmany, 1):
number = int(input("Write a number "))
result += MakeingGraphic(number) + "\n"
print result
You don't need that MakingGraphic
, just use a list to store the strings of "*":
In [14]: howmany = int(input("How many numbers will you write?"))
...: lines=[]
...: for i in range(howmany):
...: number = int(input("Write a number "))
...: lines.append('*'*number)
...: print('\n'.join(lines))
The issue of your code is, variable "list" is an integer, not a list ( don't use "list" as a variable name, because it shadows the python builtin type/function list
, use some name like lst
instead).
If you want to try function call, you can change your code to:
import math
def MakingGraphic(lst):
graphic = '\n'.join(number * '*' for number in lst)
return graphic
lst = []
howmany = int(input("How many numbers will you write?"))
for i in range(0, howmany, 1):
number = int(input("Write a number "))
lst.append(number)
result = MakingGraphic(lst)
print(result)
You could probably print the stars from the function itself instead of returning it. print will add a new line automatically. Hope that helps!
I made some changes in the code, but your problem was that you were sending a int not a list with ints:
import math
def MakingGraphic(number):
graphic = ''
for n in list:# loop to the list
graphic += n * '*' + '\n' # the \n adds a line feed
return(graphic)
list = [] # list
howmany = int(input("How many numbers will you write?"))
for i in range(0, howmany, 1):
number = int(input("Write a number "))
list.append(number) # add the number to the list
result = MakingGraphic(list)
print (result)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.