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 1 year ago.
The community reviewed whether to reopen this question 1 year ago and left it closed:
Original close reason(s) were not resolved
Improve this question
I just need to divide some number by only 10. For instance, if the number is 127, the result should only be 12 and remained to be 7. Is there a way I can get Python to give me these BOTH results? Because I need to divide that remainder by some number as well for instance 2.
So overall it should be like this:
127 = 12 (10) and 3 (2) and 1(remainder).
Hope that makes sense
You can try using double division and modulus division -
quotient = 127 // 10
remainder = 127 % 10
print(quotient) # 12
print(remainder) # 7
Or use divmod -
num = 127
d = 10 # Divisor
quotient,remainder = divmod(num, d) # It returns tuple
print(quotient) # 12
print(remainder) # 7
def divide_and_get_remainder(num, divisor):
quotient = num // divisor
remainder = num % divisor
return quotient, remainder
if __name__ == '__main__':
num = 127
divisor = 10
q, r = divide_and_get_remainder(num,divisor)
print(f'{num} / {divisor} = {q} with remainder = {r}')
Related
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 3 years ago.
Improve this question
I am writing a python script right now to check if a number a Wilson Prime, but it says I have a syntax error on line 9.
I've tried both the mod function with x%y == 0 and x/y == int, but both of them have given me a syntax error.
n = int(input("Type a natural number here"))
fact = 1
for i in range(1,n):
fact = fact * i
finalfact = fact + 1
if finalfact % n == 0
print(n, "is a prime number!")
if (finalfact/n) % n == 0
print(n, "is also a Wilson Prime!")
I'm trying to make it check if (n-1)!+1 is divisible by n (which is a way to find prime numbers) and to check if (n-1)!+1 is divisible by n^2 (Wilson Primes), but it gives me a syntax error on line 9 and nothing happens.
You are missing : at the end of if statements.
Change:
if finalfact % n == 0
To:
if finalfact % n == 0:
And:
if (finalfact/n) % n == 0
To:
if (finalfact/n) % n == 0:
Need to indent the code properly:
Change:
for i in range(1,n):
fact = fact * i
To:
for i in range(1,n):
fact = fact * i
Also:
finalfact / n # 5 / 2 = 2.5
Should be:
finalfact // n # 5 // 2 = 2
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 3 years ago.
Improve this question
Am new to programming and I just started studying and practicing. I started with python and am at for loops now. I kinda coded something that’s getting confusing for me because I can’t seem to understand how the code arrived at that output. Can someone please explain it to me. Would be very grateful.
Here’s the code I did:
a = range(1,20)
total = 0
for i in a :
if i%3==0 or i%5==0 :
total new = total + i
print (total new)
And the output was 18.
a = range(1,20)
Your program is creating a range of numbers between 1 and 19 (ranges go to one less than the max number you specify) like [1, 2, 3, 4, ..., 19]
total = 0
Your program is initializing the variable total to equal 0
for i in a:
You start looping through the range you made earlier, the first iteration i=1, the next i=2 and so on until i=19
if i % 3 == 0 or i % 5 == 0:
You are selecting only the data where the modulo of 5 or 3 is 0. For example:
3 % 3 == 0 (0 remainder)
4 % 3 == 1 (1 remainder)
Now altering your variable to a reasonable name (without spaces) that will actually utilize the variable we initialized above
total = total + i # alternatively written "total += i"
This says that every time the value i is evenly divisible by 3 or 5 we will add it to our total
print(total)
We show the final result after adding values. You incorrectly scripted your program to do this though so it only showed the largest value that was evenly divisible by 5 or 3 which is 18.
When scripted correctly:
a = range(1, 20)
total = 0
for i in a:
if i % 3 == 0 or i % 5 == 0:
total += i
print(total)
Outputs
78
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 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
I have 2 questions about Fibonacci numbers.
1) The number of integers taken as an argument into the Fibonacci base functions? (in Turkish: Argüman olarak alınan onluk tabanda ki sayıyı Fibonacci tabanına dönüştüren işlev?)
2) The number given in the Fibonacci function converts integer base? (in Turkish: Fibonacci tabanında verilmiş sayıyı onluk tabana dönüştüren işlev?)
a, b = 0, 1
print (a)
print (b)
i = 2
while i<=50:
a,b = b, a+b
print (b)
i+=1
i can wrote that, but i want such as something like f(10) or f(5)
I'm sorry, my english isn't so good. Thank you.
Just move it all to a function and handle special cases.
def fib(n):
#TODO: handle invalid case(negative or non-int) and return
if(n == 0):
print(0)
return
if(n == 1):
print(1)
return
a, b = 0, 1
i = 2
#this will go up to the n-th number
while i<=n:
a,b = b, a+b
i+=1
print (b)
Then you can do this:
fib(1)
fib(0)
fib(6)
fib(33)
And you'll get:
1
0
8
3524578
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
import random
def samplegcd (n, size ):
""" repeats the following " size " times : Choose two
random integers , n bits long each ( between 1 to 2**n -1).
Checks if they are relatively prime .
Computes the frequency and the derived approximation to pi."""
count =0
for i in range (0, size ):
if gcd ( random.randint(1,2**n - 1) ,
random.randint(1,2**n - 1)) ==1:
count += 1 # the dreaded +=
return count /size , (6* size / count )**0.5
Why does random.randint(1,2**n - 1) return the n bits long integer and not simply the integer itself?
random.randint(1,2**n - 1)
This returns random integer in a range from 1 to 2^n-1. There is nothing to do with python. It is mathematical question. If we want to generate n-bit integer (integer which has not more than n bits) then we should generate number in a range from 1 to MAX_N_BIT_VALUE. What is the maximum value which can be fit into n bits? It is 2^n-1.
maximum 2-bit value is 11 (binary) = 3 (decimal) = 2^2-1.
maximum 3-bit value is 111 (binary) = 7 (decimal) = 2^3-1.
maximum 4-bit value is 1111 (binary) = 15 (decimal) = 2^4-1.