Print a pyramid of numbers in python 3 [closed] - python

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 4 years ago.
Improve this question
Okey, I actually don't really know how to print a pyramid in python and even worse I need to get this:
Pyramid of n numbers
The length of the pyramid is asked to the user
Here's the code.
n = int(input("\n Enter the lenght of the triangle.. "))
for i in range(n):
for j in range(n - i):
print(" ", end = " ")
for k in range(i):
print("*", end = " ")
print()
Here is the output

One of the options:
import numpy as np
a = int(input())
def pyram(a):
n = np.zeros(a)
hal = int((a+2)/2)+1
n[:(hal-1)] = range(1,hal)
n[(hal-1):] = np.array(range(1,hal-1))[::-1]
print(n)
for i in range(1,a*2,2):
pyram(i)

Related

Can someone explain to me the part which is highlighted? [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 10 months ago.
Improve this question
Im already done with the part before the highlighted part, but i can't understand the part which is highlighted and i dont want to look at the code.
Something like that:
number = int(input("Your number: "))
while number != 1 :
number = collatz(number)
As I understand, it should be something along the lines:
def collatz(number):
if number // 2 == 0:
print(number // 2)
return number // 2
else:
print(3 * number + 1)
def iterate_program:
number = int(input())
while number != 1:
number = collatz(number)
And then you just call iterate_program.

why am i getting the wrong answer for the index of the number whos left sum=right sum in the list [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 1 year ago.
Improve this question
please tell my mistake
i m getting the index of the number whose leftsum=rightsum wrong
L=[]
n=int(input("enter the number of values to be added to the list: "))
for i in range(0,n):
k=int(input("enter the values: "))
L.append(k)
for i in range(0,n):
if sum(range(L[0],L[i])) == sum(range(L[i+1],L[-1])):
print(L.index(L[i]))
break
else:
print("0")
For starters, you've used range(a,b), which returns all the numbers from a to b-1
Try this:
L=[]
n=int(input("enter the number of values to be added to the list: "))
for i in range(0,n):
k=int(input("enter the values: "))
L.append(k)
for i in range(1,n):
if sum(L[0:i]) == sum(L[i+1:]):
print(L.index(L[i]))
break
elif i==n-1:
print(0)

Input multiple user inputs into an array / list [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 2 years ago.
Improve this question
Sorry if it's an easy question. But what I need to do is have a script which inputs 5 numbers into an array. For instance the user will be asked 5 times to input numbers from which the mean and sum will be calculated. Is placing the users inputs into an array the easiest way to do it and if so how?
counter = 0
number_array = []
while counter < 5:
num = float(input("Enter a number: "))
number_array.append(num)
counter += 1
total = 0
for number in number_array:
total = total + number
average = total / 5
print("The sum is: ", total)
print("The average is ", average)

Need Python Help for increasing Numbers [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 6 years ago.
Improve this question
so from this code, that is writen down below, if k is even i must +3 to K number, if it's odd i must multiply it 5 times, i dont know how to show that if k is even, anyone can help me out?
k=int(input('K= ')
if k%2: # (is this correct? if k is even)
k+=3
else:
k*=5
In python3,
k = int(input())
if k%2 == 0:
k = k + 3
else:
k = k * 5
if you are using python2, use raw_input() to take input from the user.
hope this helps.
I think you should really study some basic books about python.
You see, blocks work by indenting in python; and else needs a ":" too:
if k % 2 == 0:
k = k+3
else:
k = k*5
should work better.

translate pseudocode palindrome [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 6 years ago.
Improve this question
I have this pseudocode that I need to translate:
Prompt the user to enter a string and call it s.
Let l be the length of string
For i from 0 upto l-1:
print s[0:i]
For i from 0 upto l-1:
print s[i:l]
Print a closing statement
This is my translation:
def main():
s=(input("Please enter a string: "))
L=len(s)
for i in [0,L-1]:
print (s[0:i])
for i in [0,L-1]:
print(s[i:L])
print("This program is complete!")
main()
However, the code isn't printing correctly. Can someone help me find my error? Thank you.
You say for i in [0,L-1], but [0,L-1] is a list with two elements: 0 and L-1. What you want instead is range(0, L) or range(L):
def main():
s=(input("Please enter a string: "))
L=len(s)
for i in range(L):
print (s[:i])
for i in range(L):
print(s[i:L])
print("This program is complete!")
main()

Categories