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 12 months ago.
Improve this question
Does anybody of you know something like JetBrains Academy? Could You help me with this task? I know surely it is very simple but I really cannot do it. I have tried a lot of times.
https://hyperskill.org/learn/step/6715
Only thing that changes in the square are the insides, so just check if you're at the first line or last line.
Not the most efficient but python makes it easy.
size = 4
for i in range(size): # each loop prints a row
print(*
['*'] + # first star in the row
['*' if i in [0, size-1] else ' '] * (size-2) + # inside stars of the row
['*'] # last star in the row
)
* * * *
* *
* *
* * * *
The task creates a square of size 4 by 4.
Try this :
size = 4
for i in range(size):
if i == 0 or i == size-1:
var = "* " * size
else:
var = "* " + " " * (size-2) + "*"
print(var)
Output:
* * * *
* *
* *
* * * *
Now you can change the value of variable size and play around to make square shapes of any dimension.
Related
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 3 years ago.
Improve this question
I am making mortgage calculator, I have provided information for variables p, i, n, but get error in the equation.
p[i(1 + i) ^ n] / [(1 + i) ^ n – 1]
What you shared is not valid python code. Here is an example of code that will accomplish what you are asking:
# define function:
def CalculateMortgage(p, i, n):
# calculate numerator:
numerator = p * (i *(1+i) ** n)
# calculate denominator:
denominator = ((1+i) ** n - 1)
# calculate mortgage:
mortgage = numerator/denominator
# return result:
return mortgage
# set variables:
p = 1
i = 1
n = 1
# call function:
mortgage = CalculateMortgage(p, i, n)
# print result:
print('Your mortgage is: ' + str(mortgage))
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 4 years ago.
Improve this question
The problem is that python tells me that the syntax for the second if statement is done wrong and I have no clue why...
Don't mind that some of the code is in Danish.
I have tried to redo the entire code but with the same result...
import math
x = float(input("Indtast 1 for trekant eller 2 for cirkel: "))
if (x == 1):
a = float(input("Indtast side a: "))
b = float(input("Indtast side b: "))
c = float(input("Indtast side c: "))
s = (1/2) * (a + b + c)
areal = math.sqrt(s * (s - a) * (s - b) * (s - c))
print("")
print("Arealet er " + str(areal)
if (x == 2):
pi = 3.14
radius = float(input("indtast radius "))
omkreds = radius * 2 * pi
areal = radius * radius * pi
print("")
print("Det er arealet", areal)
print("")
print("Det er omkredsen", omkreds)
else:
print("")
print("KÆMPE FAIL! KA' DU IK' LÆSE!")
I expected that the code would run without any failures but that doesn't seem to be the case here.
This line is missing a closing parentheses:
print("Arealet er " + str(areal)
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 7 years ago.
Improve this question
i = 10
while i > 0:
print i*'*' + 2*(10-i)*' ' + i*'*'
i -= 1
for x in range(2,11):
print x* '*' + 2*(10-x)*' '+ x*'*'
x += 1
Can someone tell me what each line does?
I'll keep it very simple, because this is very basic stuff.
i = 10
Variable i is initialised as 10.
while i > 0:
print i*'*' + 2*(10-i)*' ' + i*'*'
i -= 1
While variable i is bigger than 0, it prints the string * i times, an empty space 2 * (10 - i) times and then the string * i times again. Every loop it subtracts 1 off of i, so i starts out as 10 and goes all the way down to 1. This results in the following triangle / pyramid:
********************
********* *********
******** ********
******* *******
****** ******
***** *****
**** ****
*** ***
** **
* *
for x in range(2,11):
print x* '*' + 2*(10-x)*' '+ x*'*'
x += 1
Here the variable x starts off as 2 and increments all the way to 10, which results in an upside down version of the above triangle (not exactly, but it would if you used range(1, 11) instead). Also x += 1 is redundant here, as the range function will already increment x in steps of 1 (because the step argument is omitted). You can see this for yourself by running the following code:
for x in range(1, 11):
print x
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 8 years ago.
Improve this question
Today, i have trying to resolve a small star pyramid :
Input:
5 1
Output:
*
**
***
****
Code:
x = 1
y = 0
m, e = map(int, raw_input().split())
while x < m:
print "\n" * y, "*" * e
m -= 1
e += 1
I did that but there is a better solution?? Thanks =)
I think this can be solved more easily:
stop, first = map(int, raw_input().split())
for i in range(stop - 1):
print '*' * (i + first)
just for fun >:)
class c:
def __init__(s,m,e):
s.e , s.m = sorted([e, m])
s.r = 42
def __iter__(s):
return s
def next(s):
if s.m < s.e:
t = "".join(chr(s.r) for _ in range(s.m))
s.m += 1
return t
else:
raise StopIteration
print "\n".join(c(*map(int,raw_input().split())))
n = int(raw_input())
for i in range(n): print "*"*i
This appears to do what your program intends to do, however I can't quite tell because of the issues I raised in my comment above.
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question does not appear to be about programming within the scope defined in the help center.
Closed 9 years ago.
Improve this question
Can anyone tell me why this has a syntax error? I've run this exact code before and it worked perfectly. The line in strong text is where Python tells me the syntax error is. Thanks, everyone!
import random
count = 0
while count < 10:
attackerLV = 20
attackerST = 20
attackerSK = 20
baseAtkPwr = 20
attackPWR = ((random.randint(85,100) * (baseAtkPwr + attackerLV + attackerST + attackerSK)) // 100
**defenderLV = 20**
defenderCON = 20
defenderSKa = 20
baseDefPwr = 20
defensePWR = (((random.randint(85,100)) * (baseDefPwr + defenderLV + defenderCON + defenderSKa)) // 4) // 100
damage = attackPWR - defensePWR
if damage <= 1:
damage = 1
print(str(attackPWR))
print(str(defensePWR))
print(str(damage))
print()
count = count + 1
You missed a parenthesis here:
attackPWR = ((random.randint(85,100) * (baseAtkPwr + attackerLV + attackerST + attackerSK)) // 100