mardi 27 juillet 2021

too much if statement in phyton, any way of simplifying it?

i am currently learning phyton for a while and made a program that is called 'character counter' which you input a text/word and it prints out a number e.g abc = 6 because a = 1, b = 2, c = 3 and so on.

and i found out while coding, it took me very long because i had to assign values to each alphabet (like the examples i showed before) and the whole thing took 80 more lines

is there any way i could simplify this one?

here is the code

alpha = str(input('enter something '))
i = 0

beta = list(alpha)
z = len(beta)


while z > 0:
    if beta[i] == "a":
        beta[i] = 1

    if beta[i] == 'b':
        beta[i] = 2

    if beta[i] == 'c':
        beta[i] = 3

    if beta[i] == "d":
        beta[i] = 4

    if beta[i] == 'e':
        beta[i] = 5

    if beta [i] == 'f':
         beta[i] = 6 

    if beta[i] == 'g':
        beta[i] = 7

    if beta[i] == 'h':
        beta[i] = 8
    
    if beta[i] == 'i':
        beta[i] = 9

    if beta[i] == 'j':
        beta[i] = 10
    
    if beta[i] == 'k':
        beta[i] = 11
    
    if beta[i] == 'l':
        beta[i] = 12
    
    if beta[i] == 'm':
        beta[i] = 13

    if beta[i] == 'n':
        beta[i] = 14

    if beta[i] == 'o':
        beta[i] = 15

    if beta[i] == 'p':
        beta[i] = 16

    if beta[i] == 'q':
        beta[i] = 17

    if beta[i] == 'r':
        beta[i] = 18
    
    if beta[i] == 's':
        beta[i] = 19

    if beta[i] == 't':
        beta[i] = 20

    if beta[i] == 'u':
        beta[i] = 21
    
    if beta[i] == 'v':
        beta[i] = 22

    if beta[i] == 'w':
        beta[i] = 23
    
    if beta[i] == 'x':
        beta[i] = 24
    
    if beta[i] == 'y':
        beta[i] = 25
    
    if beta[i] == 'z':
        beta[i] = 26


    i= i + 1
    z = z - 1

print(sum(beta))

i think that there is another way with simplifying, it but i currently dont know how to do it, so please anyone who know how to simplify this heck long code, please help, thank you!

Aucun commentaire:

Enregistrer un commentaire