Problem with a single line code of random digit number [duplicate] - python

This question already has answers here:
How to generate a random number with a specific amount of digits?
(10 answers)
Closed 2 years ago.
I took this line from online to make a random 4 digit number
number = str(random.randint(0,9999))
but sometimes it give me random 3 digit number why?
Is their a better way to make a random multiple digit number in python

number = str(random.randint(1000,9999))

Because you've asked for a number between 0 and 9999 inclusive. That includes single, double and triple digit numbers as well.
If you want only 4 digit numbers start at 1000:
number = str(random.randint(1000, 9999))
See the randint() documentation.
If you want 4 digits that could include leading zeroes then you use random.choices():
from string import digits
number = ''.join(random.choices(digits, k=4))
which picks 4 digits with replacement and joins them into a string.

Related

Adding comma after a character in string [duplicate]

This question already has answers here:
How to add a string in a certain position?
(10 answers)
Closed 7 months ago.
number = input("Enter the number:")
''' Let's suppose the entered number is 0145. Question is below.'''
I want to add a comma after 0.
How can i do this?
Use {:,} to format a number with commas in Python.
Example:
number = 1000
print(f"{number:,}")
Output:
 
1,000
If want a general purpose number formatter for numbers as strings that may include leading 0's then there is a solution using regular expressions here.
If user enters "0145" and want to format that as "0,145" then you'd need to use the string input rather than converting to an integer which would drop any leading 0's.

Split number into n numbers [duplicate]

This question already has answers here:
Generating a list of random numbers, summing to 1
(12 answers)
Generate random numbers summing to a predefined value
(7 answers)
Getting N random numbers whose sum is M
(9 answers)
Closed 10 months ago.
Let's say I have a number m (it will be integer, usually 1) and I want to split m into n random numbers, which have the sum m. I want to make a distribution which will be random, but will be close (doesn't have to be really close) to uniform distribution.
I have this
pieces = []
for i in range(n-1):
pieces.append(random.uniform(0,m-sum(pieces)))
pieces.append(m-sum(pieces))
The problem is, that the first number is usually really high compared to the rest of the numbers. When I tried it for m = 1 and let's say n=10, the first number was close to 0,5 and the last number was close to like 0,001 (or something like that), which is not a problem, but the problem is, that the first number is on average really big compared to all of the other numbers.
My question is: Is there some kind of way to do this, so the numbers are closer? There can be a big difference between biggest and smallest number, but I want it to feel more uniform, if that makes sense.

How to check what the last binary digit of a variable is? [duplicate]

This question already has answers here:
Python int to binary string?
(36 answers)
Check if a number is odd or even in Python [duplicate]
(6 answers)
Closed 5 years ago.
So I'm building various even-odd checkers. Standard method using python - x%2 == 0 as a boolean. Fine.
However, I've read that a different way of checking is to grab the binary string representing the number, then simply check the last digit of the string. If the last digit is 0, it's even, if it's 1, it's odd. For example:
00001011 = 11
Last digit is 1
11 is odd
00001110 = 14
Last digit is 0
14 is even.
It would amuse me to no end to be able to build even/odd checkers that use the binary string instead of a %2 check. The only thing I can't figure out, and my google-fu is failing me - how to grab and handle the string?
Threads for reference: Compare the last binary digit to 1 in MIPS assembly gives an assembly answer.
All of the other threads deal with getting the last digit of a normal number, IE last digit of 12345 is 5, and ignore the "How to read it in binary"
Try this:
def is_even(n):
if "{0:b}".format(n).endswith(0): # Checks if converted number ends with 0
return True
else:
return False

When Rounding to nearest hundreds, how do I include 0s [duplicate]

This question already has answers here:
Rounding a number in Python but keeping ending zeros
(6 answers)
Closed 6 years ago.
So let's say I have this code:
num = 1.29283
round(num, 2)
That rounds to 1.29, but if I do this:
num = 1.30293
round(num, 2)
That rounds to 1.3. I want to know if there is a way to have it round to 1.30; I know it is the same number, but I need it to print 1.30.
You can use string formatting for this. A number in python does not have such a thing as trailing zeros. So your question only make sense for strings.
Example:
>>> num = 1.30293
>>> "{:.2f}".format(num)
'1.30'
The .2f says that this is a float (f) and that you want two digits after the point .2. Read more about string formatting here

Why isn't this code working ? (Python) [duplicate]

This question already has answers here:
How can I read inputs as numbers?
(10 answers)
Closed 7 years ago.
So I'm making a code that accepts a number with only 8 or 7 digits and then if the user enters an 8 digit number then it should add all 8 digits together then divide by 10 and print out the answer. I have been trying to change the user's input into a list but it hasn't been working out.
My current code (not working):
NumGiven=''
while not NumGiven.isnumeric():
NumGiven=(input('Please enter a 7 or 8 digit number:'))
while len(NumGiven)<7 or len(NumGiven)>8:
NumGiven=(input('Please enter a 7 or 8 digit number:'))
if len(NumGiven)==8:
list=[int(i) for i in NumGiven.split()]
I think there is something wrong with the last line, I looked at many other topics but they never seemed to work. Can some one help me tweak this code.
NumGiven.split() splits on whitespace, but there probably isn't any. Since you want to iterate over characters, you can just eliminate the .split().
list=[int(i) for i in NumGiven]
OP asked for the sum - it should be:
print(sum([int(i) for i in NumGiven])/10.0)

Categories