How to increment a variable in Python?

Asked

Viewed 2,536 times

3

I’m trying to make a show where he throws a letter on the screen you press it and supposedly you should get a point so I know this is probably a really stupid question, but I tried it in a lot of ways and I couldn’t.

import random


def main():
    x = random.randint(0,25)
    ponto = 0

    letra = ['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']

    rletra = letra[x]

    print (rletra)


    print ('Sua pontuação é:', ponto)

    a = input('')

    if a == rletra:

        ponto += 1
        print ('Sua pontuação é:', ponto)

        main()

    else:
        print('errado')
main()

The only thing that happens is that the point value goes to 1 and does not increase.

  • 1

    Is there any way to adjust the indentation of your code? Otherwise, you can’t know if the code is wrong or it’s a flaw in the indentation.

  • Instead of recursiveness, which doesn’t make much sense in this case, why not a repetition loop?

  • It’s just that I don’t really know how to format code around here, but I think it’s right now

  • @Andersoncarloswoss and how would I do that? using a for?

  • And when should it close? Without a criterion it would never end, this is what you want?

  • @Maniero ends only when the user misses the letter

Show 1 more comment

1 answer

4


The question is not silly, and is doing as it should, learn from basic things. Maybe the mistake was just trying something without knowing, started using recursiveness by accident. This means that you need to go back a little bit earlier to know some more basic things, and learn to make one of the simplest loops that exists which is the unconditional loop to end up spelled out in it.

Of course, it has to end sometime and in comment it was described when it should end, and then the existing condition itself can be used to end, because the if existing meets the end very well. Just need to learn the break which forms a bond at that moment and usually only makes sense within a if even, that is to say, be conditional.

The way I was doing creating a new scope in each run, and everything happened from scratch, so the score was zeroed every time. Thus it is more controlled because some instructions are only executed once, and does not create new scope, so the variable keeps its value.

I improved some other things, I hope it serves as a learning.

import random

def main():
    pontos = 0
    letra = ['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']
    while True:    
        rletra = letra[random.randint(0, 25)]
        print(rletra)
        if input('') == rletra:
            pontos += 1
            print('Sua pontuação é:', pontos)
        else:
            print('errado')
            break
main()

Behold working in the ideone. And in the repl it.. Also put on the Github for future reference.

  • 1

    Thanks, I was able to understand the part about creating a new scope every time I closed the if

  • have to make recursive, but is not suitable for this and too complicated. Now you can vote on everything on the site also.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.