How to give a space along with printing result - python-3.x

So, i want to create a function. If i use 4 as input number, then it will give a result:
****
***
**
*
I use code like this.
def print_pattern(input_number):
for i in reversed(range(input_number)):
integer = (i+1)
if integer == input_number:
output = (integer * '*')
elif integer < input_number:
output = " " + (integer * '*')
print(output)
print_pattern(6)
But, the result only show the first looping like this.
****
***
**
*
what should i do?

Using str.rjust
Ex:
def print_pattern(input_number):
num = input_number+1
for i in reversed(range(num)):
print(("*"*i).rjust(input_number, " "))
print_pattern(4)
Output:
****
***
**
*

def print_pattern(input_number):
for i in range(input_number):
print((" " * i) + "*" * (input_number -i))
Examples:
>>> print_pattern(4)
****
***
**
*
>>> print_pattern(6)
******
*****
****
***
**
*

Related

Python: a pattern of two signs (* and $) that creates a pyramid

I want to create a pattern that looks like this:
*
* $
* $ *
* $ * $
* $ * $ *
* $ * $
* $ *
* $
*
I know how to use nested loops to print pattern like this with one sign but I have difficulties to create it wth two signs.
Any idea how to do that?
I am using python3 here
for i in range(5):
for j in range(i+1):
print('*' if j%2==0 else '$' , end=" ")
print()
for i in range(4):
for j in range(4-i):
print('*' if j%2==0 else '$' , end=" ")
print()

How can we solve dynamic pattern program for odd no of values?

what is the solution of the pattern program given below.
like if we have, n=3 then pattern should be
*
* *
*
for n=5
*
* *
* * *
* *
*
and so on for n= odd values
here is answer :
Enter any odd no of your choice
n= int(input("Enter the number:"))
floor division to divide the pattern into two parts
z= n//2
for i in range(z+1):
print(" " *(z-i) + "* " *(i+1))
for i in range(z):
print(" " *(i+1) + "* " *(z-i))

why compiler doesn't stop iterating while loop

I made a program to print pyramid using * with my own way but whenever i run my program on compiler the program gets executed and doesn't stop after the for loop ends its iteration,here's my program which i think should stop executing after 10 iterations.
a = " "
b = ""
for i in range(10):
a = a[:-1]
b = (b * i) + '*'
print('\n')
for k in range(i):
print("{}{}".format(a,b), end="")
i am expecting the output like this:
*
***
*****
*******
*********
***********
*************
***************
Your problem probably comes from your b assignation.
Let's calculate the length of b at step i (from 0 to 10) (b(n) = b(n-1)*index + 1)
b(0) = 0*0 + 1 = 1
b(1) = 1*1 + 1 = 2
b(2) = 2*2 + 1 = 5
b(3) = 5*3 + 1 = 16
And so on, we can see the length of b is going exponential, for instance, for index = 10, len(b) = 986410
This increasing string might make the compiler slowing down.
You might want to try this code which does not keep in memory the strings.
height = 10
for i in range(height):
print(' '*(height-i-1)+'*'*(2*i+1))
output:
*
***
*****
*******
*********
***********
*************
***************
*****************
*******************
Try this improved code instead:
a = " " # empty string to be attached at the front and back (length is 11)
for i in range(10): # height of pyramid
a = a[:-1] # make string a bit smaller to compensate for
b = '*' * (i*2 + 1) # the increasing amount of '*'s
print("{}{}{}".format(a, b, a)) # prints the pyramid
The len(a) should be the height of your pyramid plus the amount of padding wanted on the last row. For example:
*
***
*****
*******
There's 2 spaces left on the bottom row, and the height of the pyramid is 4. That means the length of a should be 6. Make a using a = ' '*(height + padding)

Having a Python if statement syntax error, assistance please

I'm currently working on some python code but its giving me a syntax error in the second 'if statement'. When I run it, it highlights the 'quadpos'. What seems to be the problem?
print(' Hello, press enter to continue')
print('What is your A value?') # Asking for the A value
myA = input()
myA = int(myA)
print('What is your B value?') # Asking for the B value
myB = input()
myB = int(myB)
print('What is your C value?') # Asking for the C value
myC = input()
myC = int(myC)
quad = ((myB * myB) - (4 * myA * myC))
if int(quad) < int(0):
print('Cannot process Value')
else:
quad2 = (int(quad)**(1/2.0))
if int(myA) > int(0):
quadneg = ((int(-myB) - int(quad2)) / (2 * myA)
quadpos = ((int(-myB) + int(quad2)) / (2 * myA)
print(int(quadneg))
print(int(quadpos))
else:
quad3 = ((int(-myB) * int(-myB)) + int(quad2)) / (2 * myA)
print (int(quad3))
The error is in the line 21 and 22 according to your attached picture
print( int( quardneg) )
print( int(quardpos) )
followed by else statement
The syntax of if else statements are :
If condition:
Code...
Else
Code ..
You are doing:
If condition:
Code
Code -- Error is here
Else
Code.
You can't put the code in the same indentation of if statement, if you do so then you have to replace else by next if.
You seem to have a missing bracket.
quadneg = ( (int(-myB) - int(quad2)) / (2 * myA) )
quadpos = ( (int(-myB) + int(quad2)) / (2 * myA) )

Python "int object is not callable"

TypeError: 'int' object is not callable
Anyone knows how can I fix this error? I know error is somewhere in the equation in the total. Thanks
decimal = 0
rate = 0
principal = 0
years = 0
def simple(p, r, n,):
decimal = r / 100
print("Principal: " + str(p))
print("Rate: " + str(decimal))
print("Number of Years: " + str(n))
total = p (1 + (decimal * n))
print("Total: " + str(total))
def main():
principal = int(input("Enter Principal: "))
rate = float(input("Enter Rate: "))
years = int(input("Enter Numbers of Years: "))
simple(principal, rate, years)
main()
print("End of Program")
Here p is an integer that you try to call :
total = p (1 + (decimal * n))
I think that you want :
total = p*(1 + (decimal * n))
On this line p is expected to be a function because it is immediately followed by a parenthesis:
total = p (1 + (decimal * n))
But p is passed as a parameter above, so I'm guessing you are passing an integer. If you mean to multiply:
total = p * (1 + (decimal * n))
You should define p first simple(int p,int r,int t) then total=p*(1+decim

Resources