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 5 years ago.
Improve this question
The following python coding is written to generate the triangle numbers between 1 to 55. But the coding is not working why ?
num = 1
sum = 0
while (num <= 10)
sum = sum + num
num = num + 1
print (sum, end=' ')
Missing colon :
num = 1
sum = 0
while (num <= 10):
sum = sum + num
num = num + 1
print (sum, end=' ')
or
num = 1
sum = 0
while (num <= 10):
sum = sum + num
num = num + 1
print (sum, end=' ')
Output
1 3 6 10 15 21 28 36 45 55
For 2.7
num = 1
sum = 0
while (num <= 10):
sum = sum + num
num = num + 1
print sum,
Your error is an error that is not followed by a while statement, followed by a () wrap and end of print that are not supported by default in Python 2.x.
The Corrected Code is:
num = 1
sum = 0
while (num <= 10):
sum = sum + num
num = num + 1
print sum
Related
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 1 year ago.
Improve this question
n = int(input())
num = n
cnt = 0
while True:
a = num // 10
b = num % 10
c = (a + b) % 10
num = (b * 10) + c
cnt += 1
if num == n:
print(cnt)
break
C:/Users/ADMIN/AppData/Local/Programs/Python/Python310/python.exe c:/Users/ADMIN/loc-git/Study_CodingTest/baekjoon/Bronze/1110번.py
Traceback (most recent call last):
File "c:\Users\ADMIN\loc-git\Study_CodingTest\baekjoon\Bronze\1110번.py", line 2, in <module>
n = int(input())
ValueError: invalid literal for int() with base 10: '& C:/Users/ADMIN/AppData/Local/Programs/Python/Python310/python.exe c:/Users/ADMIN/loc-git/Study_CodingTest/baekjoon/Bronze/1110번.py'
python version = 3.10.0
I Dont't understand what you need. but chack this code don't come any error:-
n = int(input("enter number"))
num = n
cnt = 0
while True:
a = num // 10
b = num % 10
c = (a + b) % 10
num = (b * 10) + c
cnt += 1
if num == n:
print(cnt)
break
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 2 years ago.
Improve this question
I am a beginner in Python and just starting to learn. Please help me with the below code.
def sum_divisors(n):
sum = 0
divisor = 1
while divisor < n
if n % divisor = 0
sum = sum + divisor
divisor += 1
# Return the sum of all divisors of n, not including n
return sum
print(sum_divisors(0))
# 0
print(sum_divisors(3)) # Should sum of 1
# 1
print(sum_divisors(36)) # Should sum of 1+2+3+4+6+9+12+18
# 55
print(sum_divisors(102)) # Should be sum of 2+3+6+17+34+51
# 114
However, I am getting the below error, please help:
Error on line 4:
while divisor < n
^
SyntaxError: invalid syntax
You missed the trailing ":": while divisor < n:.
There are also various problems with the indentation.
This may fix your code:
def sum_divisors(n):
sum = 0
divisor = 1
while divisor < n:
if n % divisor == 0:
sum = sum + divisor
divisor += 1
# Return the sum of all divisors of n, not including n
return sum
It's simple:
def sum_divisors(n):
sum = 0
divisor = 1
while divisor < n:
if n % divisor == 0:
sum = sum + divisor
divisor += 1
It's simple syntax: there's supposed to be a ":" after the while and if statements.
And indentation after that.
Also == for if statements
You need a colon (:) after the while and if statements and proper indentation, and the = is for variable assigning, the operator for equality is ==:
def sum_divisors(n):
sum = 0
divisor = 1
while divisor < n:
if n % divisor == 0:
sum = sum + divisor
divisor += 1
You need to put : after while,if,for and function definitions. Also you need to check the equality with == instead of =.
def sum_divisors(n):
sum = 0
divisor = 1
while divisor < n:
if n % divisor == 0:
sum = sum + divisor
divisor += 1
# Return the sum of all divisors of n, not including n
return sum
There were few mistakes of missing colon. I have fixed them.
def sum_divisors(n):
sum = 0
divisor = 1
while divisor < n:
if n % divisor == 0:
sum = sum + divisor
divisor += 1
# Return the sum of all divisors of n, not including n
return sum
print(sum_divisors(0))
# 0
print(sum_divisors(3)) # Should sum of 1
# 1
print(sum_divisors(36)) # Should sum of 1+2+3+4+6+9+12+18
# 55
print(sum_divisors(102)) # Should be sum of 2+3+6+17+34+51
# 114
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
Write a program that estimates the value of the mathematical constant e by using the formula
[Note: Your program can stop after summing 10 terms.]
e = 1 + 1/1! + 1/2! + 1/3! + ...
I am new to programming languages and trying to learn python by myself. This question has been asked and answered before but I am seeking a solution without any function or module and I want to use only while or for loop.
Edit: This is the code that I wrote for calculating a factorial:
n = 0
factorial = 1
n = raw_input( "Enter a positive integer: ")
n = int(n)
while n < 0:
print "Enter a POSITIVE integer: "
backup = n
backup = int(backup)
while n != 0:
factorial *= n
n -= 1
print "%d!= %d" % (backup, factorial)
And this could be funny to most of you but this is the code that I wrote for the question but it ended up with a syntax error:
accuracy = 1
factorial = 1
counter = 1
e = 1.0
accuracy = raw_input( "Enter desired accuracy of e: ")
accuracy = int (accuracy)
while (counter <= (accuracy - 1))
factorial = factorial * counter
e = e + ( 1 / float(factorial))
counter += 1
print "Constant e is: ", e
Your first step is writing a factorial function. This can be done recursively or with a for-loop:
def factorial(n):
return 1 if n < 2 else n * factorial(n-1)
or:
def factorial(n):
x = 1
for i in range(2, n+1):
x *= i
return x
and then for calculating e we can again use recursion or a for-loop. This function takes a parameter which indicates how many terms in the formula it should evaluate (so you should call with 10 in your case).
def calc_e(t):
if not t:
return 0
else:
return 1 / factorial(t-1) + calc_e(t-1)
or:
def calc_e(t):
s = 0
for i in range(t):
s += 1 / factorial(i)
return s
and they both work:
>>> calc_e(1)
1.0
>>> calc_e(2)
2.0
>>> calc_e(3)
2.5
>>> calc_e(4)
2.6666666666666665
>>> calc_e(5)
2.708333333333333
>>> calc_e(10)
2.7182815255731922
>>> calc_e(20)
2.7182818284590455
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 5 years ago.
Improve this question
I have a very simple program.
count = 0
total = 0
def iseven(number):
if number % 2 == 0:
True
else:
False
while count < 10 :
if iseven(count):
total = total * 2
else:
total = total * 4
print total
count = count + 1
print "final total is ", total
But this just prints zero on every iteration, and the final total is then zero.
So looks like the total value is not being updated.
Any ideas?
count = 0
total = 1
def iseven(number):
if number % 2 == 0:
return True
else:
return False
while count < 10 :
if iseven(count):
total = total * 2
else:
total = total * 4
print (total)
count = count + 1
print ("final total is ", total)
You are multiplying 0, which always results in 0
Output:
2
8
16
64
128
512
1024
4096
8192
32768
final total is 32768
Your is_even function is missing a return statement.
Also, total should be initialised to 1, instead of 0 (1 is the idempotent value for the multiplication, not 0).
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
Essentially, if the user inputs 2,12, the output should be 2 + 3 + 4 + 5 + 6 + 7 + 9 + 10 + 11 + 12.
num1 = int(input("Please enter a number between 1 and 10: "))
num2 = int(input("Please enter a number between 11 and 20: "))
addition = num1 + num2
print (addition)
sum = 0
count = 1
while (count <= num1):
sum = sum + 1
count = count + 1
print ("Your total price comes to ", total_price)
Try the following code:
num1 = int(input("Please enter a number between 1 and 10: "))
num2 = int(input("Please enter a number between 11 and 20: "))
the_sum = 0
start = num1
end = num2 + 1
m = start
while m < end:
the_sum += m
m += 1
print ("Your total price comes to ", the_sum)
You have several problems... including that you are not actually doing anything. All you really do is do the addition line and printing what it is. Naturally, you should start with a for loop that will loop through the limits set by the user:
for i in range(num1, (num2)+1):
Now to add the numbers in between and keep track of the current sum, let's create two variables to keep track:
current_sum = 0
number = num1
for i in range(num1, (num2)+1):
Now add number to current_score and add one to number:
current_sum = 0
number = num1
for i in range(num1, (num2)+1):
current_score += number
number += 1
Then finally print the result:
current_sum = 0
number = num1
for i in range(num1, (num2)+1):
current_score += number
number += 1
print current_sum