leng=int(input())
def gerRandomString(leng):
import random
num=random.randrange(97,123)
for i in range(1,leng+1):
num
num=chr(num)
word=[]
for i in range(0,leng):
word.append(num)
b="".join(word)
print(b)
gerRandomString(leng)
If you run it here, you'll get the same number of characters you've entered over and over again, but how do you get different characters to get the number you've entered? It doesn't matter if it's a duplicate character.
Example: abjunica at 8 input, and ackde at 5 input
python
Python 3.8.5 (tags/v3.8.5:580fbb0, Jul 20 2020, 15:57:54) [MSC v.1924 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license()" for more information.
>>> chr(97)
'a'
>>> chr(123)
'{'
>>> [ chr(e) for e in range(97,123) ]
['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z']
>>> import random
>>> random.sample('abcdefghijklmnopqrstuvwxyz', 3)
['z', 'c', 'q']
>>> l = 5
>>> random.sample('abcdefghijklmnopqrstuvwxyz', l)
['j', 'x', 'w', 'e', 'z']
>>> ''.join(random.sample('abcdefghijklmnopqrstuvwxyz', l))
'rmevu'
>>> l = 10
>>> ''.join(random.sample('abcdefghijklmnopqrstuvwxyz', l))
'dpilbfryth'
>>> ''.join(random.sample('abcdefghijklmnopqrstuvwxyz', l))
'jsnuwfvibl'
>>> l = 33
>>> ''.join(random.sample('abcdefghijklmnopqrstuvwxyz', l))
Traceback (most recent call last):
File "<pyshell#12>", line 1, in <module>
''.join(random.sample('abcdefghijklmnopqrstuvwxyz', l))
File "C:\PROGRAMS\Python3864\lib\random.py", line 363, in sample
raise ValueError("Sample larger than population or is negative")
ValueError: Sample larger than population or is negative
>>> help(random.sample)
Help on method sample in module random:
sample(population, k) method of random.Random instance
Chooses k unique random elements from a population sequence or set.
Returns a new list containing elements from the population while
leaving the original population unchanged. The resulting list is
in selection order so that all sub-slices will also be valid random
samples. This allows raffle winners (the sample) to be partitioned
into grand prize and second place winners (the subslices).
Members of the population need not be hashable or unique. If the
population contains repeats, then each occurrence is a possible
selection in the sample.
To choose a sample in a range of integers, use range as an argument.
This is especially fast and space efficient for sampling from a
large population: sample(range(10000000), 60)
>>> help(random.choices)
Help on method choices in module random:
choices(population, weights=None, *, cum_weights=None, k=1) method of random.Random instance
Return a k sized list of population elements chosen with replacement.
If the relative weights or cumulative weights are not specified,
the selections are made with equal probability.
>>> l = 33
>>> ''.join(random.choices('abcdefghijklmnopqrstuvwxyz', l))
Traceback (most recent call last):
File "<pyshell#16>", line 1, in <module>
''.join(random.choices('abcdefghijklmnopqrstuvwxyz', l))
File "C:\PROGRAMS\Python3864\lib\random.py", line 400, in choices
cum_weights = list(_accumulate(weights))
TypeError: 'int' object is not iterable
>>> ''.join(random.choices('abcdefghijklmnopqrstuvwxyz', k=l))
'lrrhjwyjxjhmzzhznalkzfqsczrwaarzr'
>>> l = 10
>>> ''.join(random.choices('abcdefghijklmnopqrstuvwxyz', k=l))
'cdekxhpcil'
>>>
563 rails db:create error: Could not find mysql2-0.5.4 in any of the sources
589 GDB gets version error when attempting to debug with the Presense SDK (IDE)
571 PHP ssh2_scp_send fails to send files as intended
562 Who developed the "avformat-59.dll" that comes with FFmpeg?
854 When building Fast API+Uvicorn environment with PyInstaller, console=False results in an error
© 2024 OneMinuteCode. All rights reserved.