Mar-17-2019, 11:47 AM
I need to use the random.randrange function to randomly select a character from a string the string is the alphabet. I cant use random choice
random.randrange
|
Mar-17-2019, 11:47 AM
I need to use the random.randrange function to randomly select a character from a string the string is the alphabet. I cant use random choice
Mar-17-2019, 12:11 PM
import string print(string.ascii_letters) # index access print(string.ascii_letters[0]) n_letters = len(string.ascii_letters) index = random.randrange(0, n_letters + 1) # read the documentation about randint and randrange to understand the differenceI think you'll find out the rest.
Almost dead, but too lazy to die: https://sourceserver.info
All humans together. We don't need politicians!
Mar-17-2019, 12:27 PM
import random
alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmopqrstuvwxyz" random_characters = len(alphabet) index = random.randrange(0, random_characters + 1) print(index) I'm a bit confused. I have to use the variable alphabet(I'm new to python 1 week in)
Mar-17-2019, 02:30 PM
Maybe I am missing something but from documentation: random.randrange
Quote:random.randrange(stop) Why random.randrange(0, random_characters + 1) and not justrandom.randrange(len(alphabet)) . Latter will give random number which is in range of valid indexes:>>> a = [1, 2, 3] >>> len(a) 2 >>> max(random.randrange(0, len(a) + 1) for x in range(100)) 3 >>> max(random.randrange(len(a)) for x in range(100)) 2 >>> a[3] Index Error /../
I'm not 'in'-sane. Indeed, I am so far 'out' of sane that you appear a tiny blip on the distant coast of sanity. Bucky Katt, Get Fuzzy
Da Bishop: There's a dead bishop on the landing. I don't know who keeps bringing them in here. ....but society is to blame.
Mar-17-2019, 02:45 PM
(This post was last modified: Mar-18-2019, 02:32 AM by ravioli2929.)
i need the specfic letter for example 0 will be A.
I need the output to show A
Mar-17-2019, 02:57 PM
I think that I do understand. Just if you have
random.randrange(0, len(alphabet) + 1) then:>>> alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmopqrstuvwxyz" >>> len(alphabet) 51 >>> max(random.randrange(0, len(alphabet) + 1) for x in range(1000)) 51 >>> alphabet[51] Index Error /../ IndexError: string index out of rangeThis means that random.randrange can return you index which is out of range. Regarding conditions: must all letters be unique or can there be duplicates?
I'm not 'in'-sane. Indeed, I am so far 'out' of sane that you appear a tiny blip on the distant coast of sanity. Bucky Katt, Get Fuzzy
Da Bishop: There's a dead bishop on the landing. I don't know who keeps bringing them in here. ....but society is to blame.
Mar-17-2019, 03:08 PM
(This post was last modified: Mar-17-2019, 03:08 PM by ravioli2929.)
there can be duplicates
unfortunately i cant use the for or in functions as it is homework.
Don't make your own alphabet string.
You have forgotten the n. import string print(string.ascii_letters)The IndexError is raised, because you try to access the 51th element, but the indexing starts with 0. This means, if you have 51 elements/letters, the last letter in this list is at index 50. The first letter is at index 0. You need to learn more about slicing: https://stackoverflow.com/questions/5092...e-notation Read the documentation about randrange and randint. For randrange you can use the output from len. With randint the stop value is inclusive. This means it is len(your_strig) + 1.
Almost dead, but too lazy to die: https://sourceserver.info
All humans together. We don't need politicians!
Mar-17-2019, 03:27 PM
Your nearly there in your code post #3
Just add alphabet[index] import random alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmopqrstuvwxyz" random_characters = len(alphabet) index = random.randrange(0, random_characters + 1) print(index) print(alphabet[index])A little fancier output ![]() import random alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmopqrstuvwxyz" random_characters = len(alphabet) index = random.randrange(0, random_characters + 1) print(f'Random characters at index <{index}> give output <{alphabet[index]}>')
Mar-17-2019, 03:42 PM
(This post was last modified: Mar-17-2019, 03:43 PM by ravioli2929.)
thanks for the help guys relatively new to this. Just asking how would I ask for more than one value say I
wanted 6 different random letters (there can be a duplicate) |
|
Possibly Related Threads… | |||||
Thread | Author | Replies | Views | Last Post | |
[split] why can't i create a list of numbers (ints) with random.randrange() | astral_travel | 7 | 2,977 |
Oct-23-2022, 11:13 PM Last Post: Pedroski55 |
|
Remove space after random.randrange(1, 11) | rs74 | 4 | 3,301 |
Jun-06-2020, 07:46 AM Last Post: rs74 |