Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
random.randrange
#8
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!
Reply


Messages In This Thread
random.randrange - by ravioli2929 - Mar-17-2019, 11:47 AM
RE: random.randrange - by DeaD_EyE - Mar-17-2019, 12:11 PM
RE: random.randrange - by ravioli2929 - Mar-17-2019, 12:27 PM
RE: random.randrange - by perfringo - Mar-17-2019, 02:30 PM
RE: random.randrange - by ravioli2929 - Mar-17-2019, 02:45 PM
RE: random.randrange - by perfringo - Mar-17-2019, 02:57 PM
RE: random.randrange - by ravioli2929 - Mar-17-2019, 03:08 PM
RE: random.randrange - by perfringo - Mar-17-2019, 04:16 PM
RE: random.randrange - by DeaD_EyE - Mar-17-2019, 03:26 PM
RE: random.randrange - by snippsat - Mar-17-2019, 03:27 PM
RE: random.randrange - by ravioli2929 - Mar-17-2019, 03:42 PM
RE: random.randrange - by ravioli2929 - Mar-17-2019, 04:20 PM
RE: random.randrange - by perfringo - Mar-17-2019, 04:27 PM
RE: random.randrange - by ravioli2929 - Mar-17-2019, 04:33 PM
RE: random.randrange - by perfringo - Mar-17-2019, 04:49 PM

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 1,652 Oct-23-2022, 11:13 PM
Last Post: Pedroski55
  Remove space after random.randrange(1, 11) rs74 4 2,385 Jun-06-2020, 07:46 AM
Last Post: rs74

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020