There's an error in the process of receiving numbers [closed] - python

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

Related

NameError: name 'number' is not defined [duplicate]

This question already has answers here:
Why does the expression 0 < 0 == 0 return False in Python?
(9 answers)
Why does this not work as an array membership test? [duplicate]
(1 answer)
Closed 4 years ago.
I have a code like below
a=25
b=20
#number=0
if a < b == True:
number = 1
elif a >b == True:
number = 2
print(number)
When i execute this i get the following error
NameError: name 'number' is not defined
When i initialize the number = 0 like below
a=25
b=20
number=0
if a < b == True:
number = 1
elif a >b == True:
number = 2
print(number)
then i am not getting the output as 2, i am getting 0 instead, what am i missing here
Put a parenthesis around the condition.
Ex:
a=25
b=20
#number=0
if (a < b) == True:
number = 1
elif (a > b) == True:
number = 2
print(number)
or:
if a < b:
number = 1
elif a > b:
number = 2
print(number)
You are currently doing (a < b) and (b == True) & (a > b) and (b == 20)

Writing a program that estimates math constant e without using any functions [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 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

Why the following python is not working? [closed]

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

Python. Noob. Syntax error [duplicate]

This question already has answers here:
Else Syntax Error Python
(7 answers)
Closed 6 years ago.
A = int(input()) #recom. sleep time
B = int(input()) #unrecom. sleep time
H = int(input()) #fact. slept
if (A <= H < B ):
print('Fine')
elif (A < H >= B):
print('too much')
else (A > H < B):
print('not much')
Error:
File "<ipython-input-9-12133f4a4c3d>", line 8
else(A > H < B):
^
SyntaxError: invalid syntax
Guys don't be rude, I'm financial and start learning programming :)
I can't see the difference between code who passed errors check and this one.
You are using else to make a test. You should use elif
A = int(input()) #recom. sleep time
B = int(input()) #unrecom. sleep time
H = int(input()) #fact. slept
if A <= H < B :
print('Fine')
elif (A < H >= B):
print('too much')
elif A > H < B:
print('not much')

How to print factors of a number? [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 9 years ago.
Improve this question
I have a question about multiples in python. Does anyone know how I can get the program to print multiples of a certain number? Like if I had them in put "10", it should print "1,10,2,5" or something along those lines
Thanks
Very naively you can test every number up to n(10)
n = 10
results = []
for i in range(1,n+1):
if n % i == 0:
results.append(i)
print(results)
Or as a list comprehension:
n = 10
print([x for x in range(1,n+1) if n % x == 0])
But in reality you only need to test up to the sqrt of n. Using a simple generator:
def divisor(n):
a = 1
l = n ** 0.5
while a <= l:
if n % a == 0:
if a == n//a:
yield a,
else:
yield a, n//a
a += 1
print([x for a in divisor(10) for x in a])
print(sorted(x for a in divisor(10) for x in a)) # Sorted
Here you go:
from collections import defaultdict
from math import sqrt
def factor(n):
i = 2
limit = sqrt(n)
while i <= limit:
if n % i == 0:
yield i
n = n / i
limit = sqrt(n)
else:
i += 1
if n > 1:
yield n
def factorGenerator(n):
d=defaultdict(int)
for f in factor(n):
d[f]+=1
return [(e,d[e]) for e in sorted(d.keys())]
def divisorGen(n):
factors = factorGenerator(n)
nfactors = len(factors)
f = [0] * nfactors
while True:
yield reduce(lambda x, y: x*y, [factors[x][0]**f[x] for x in range(nfactors)], 1)
i = 0
while True:
f[i] += 1
if f[i] <= factors[i][1]:
break
f[i] = 0
i += 1
if i >= nfactors:
return
print list(divisorGen(10))
Prints:
[1, 2, 5, 10]

Categories

Resources