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()
Related
I can't understand logic of the last line of the below mentioned code. The code is a recursive function that gives you Factorial of a number. What does this(factorial(n-1))return each time?
def factorial(n): if n<=1: return 1 else: return n*factorial(n-1)
=================================================================
If you're calculating the factorial of something you are doing
1 * 2 * ... * n
Another way of representing this is
n * (n-1) * (n-2) * ... * 1
Or more simplified
n * (n-1) * ((n-1)-1) * ... * 1
Looking at that last bit you should be able to see that if the number is 1 the answer is 1. Otherwise it's the a number n times whatever the result of the factorial of (n-1) is.
That's exactly what this function does.
def factorial(n): # declare the function
if n<=1:
return 1 # if n is 1 or lower, the answer should be 1.
else:
# otherwise, the answer is the result of n * (all these steps again for (n-1))
return n*factorial(n-1)
Say you give factorial(5) what actually happens is
factorial(5) = 5 * factorial(4)
= 5 * (4 * factorial(3))
= 5 * (4 * (3 * factorial(2)))
= 5 * (4 * (3 * (2 * factorial(1))))
= 5 * (4 * (3 * (2 * 1)))
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))
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)
******
*****
****
***
**
*
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) )
here is my code
def printTriangleRecursive(width):
# Recursive implementation
if width > 1:
printTriangleRecursive(width - 1)
print ("*" * width, end=" ")
Am suppose to get this using recursion
*
* *
* * *
* * * *
* * * * *
but am getting a different staff
By default, print will issue a newline after printing the text. But with
end=" "
you are overriding that behavior and telling it to print a space instead of a newline.
To fix, just delete that
def printTriangleRecursive(width):
# Recursive implementation
if width > 1:
printTriangleRecursive(width - 1)
print ("*" * width)