-4

I need help with python program. I don't know how to make python change at least 1 lowercase letter to uppercase.

from random import *
import random

pin=""
lenght=random.randrange(8,15)

for i in range(lenght):
    pin=pin+chr(randint(97,122))


print(pin)

ado
  • 5
  • 4
  • 3
    My guess is that by "at least 1" you mean "at least 1 but not all". Is that correct? – John Coleman Nov 08 '22 at 14:01
  • 2
    Are you trying to change the case of an existing string or construct a new string with random characters drawn from both lower & upper cases? Either way, the `strings` library contains some useful constants: https://docs.python.org/3/library/string.html – Sarah Messer Nov 08 '22 at 14:02
  • your imports are a bit odd, to say the least. looks like you're importing everything from `random` module and then importing the module itself `import random`. Not sure if it's the best approach here to mix them together - you probably want one or the other, not both. – rv.kvetch Nov 08 '22 at 14:04

3 Answers3

2

You want a password with at least one uppercase letter but it shouldn't be every character. First get length random lowercase letters. Then get some random indexes (min. 1, max. length-1) that should be transposed to uppercase.

import random
import string

# randomize length of password
length = random.randrange(8, 15)

# create a list of random lowercase letters
characters = [random.choice(string.ascii_lowercase) for _ in range(length)]

# select the indexes that will be changed to uppercase characters
index_upper = random.sample(range(0, length), random.randrange(1, length))

# construct the final pin
pin = ''.join(c.upper() if i in index_upper else c for i, c in enumerate(characters))

You can check what's going on if you print the variables

print(length)
print(characters)
print(index_upper)
print(pin)

Example output:

13
['y', 'g', 'u', 'k', 't', 'a', 'w', 'a', 'g', 'f', 'f', 'x', 'p']
[2, 7, 4, 0, 5, 6, 1]
YGUkTAWAgffxp

If you're not familiar with the generator syntax where pin is build, it roughly translates to:

pin = ''
for i, c in enumerate(characters):
    if i in index_upper:
        pin += c.upper()
    else:
        pin += c
Matthias
  • 12,873
  • 6
  • 42
  • 48
0
import random

pin = []
length = random.randrange(8, 15)

for i in range(length):
    pin.append(chr(random.randint(97, 122)))

number_of_uppercase_chars = random.randint(1, length)

for i in range(number_of_uppercase_chars):
    char_index = random.randint(0, length - 1)
    while pin[char_index].isupper():
        char_index = random.randint(0, length - 1)

    pin[char_index] = pin[char_index].upper()


pin = "".join(pin)
print(pin)
ChamRun
  • 104
  • 6
  • This could change `0` characters due to `random.randint(0, length)`. And the name `number_of_uppercase_chars` isn't true either, because your algorithm could select the same index multiple times. – Matthias Nov 08 '22 at 14:28
0
from random import *
import random


pin=""
lenght=random.randrange(8,15)

for i in range(lenght):
    pin=pin+chr(randint(97,122))

print(pin)

pin = pin.upper()

while not any(c for c in pin if c.islower()):
    pin = pin.lower()
    pin = "".join(random.choice([k.upper(), k ]) for k in pin )

print(pin)
Aymen
  • 841
  • 1
  • 12
  • 21