I have a problem with the beginner python code

Asked

Viewed 85 times

-1

Good afternoon! I’m a beginner and I’m trying to learn python, I tried to make a program that calculated if the number typed is prime and then the program asks if I would like to calculate all the primes before it put when it arrives in the while loop it keeps repeating forever regardless of the answer I give

I would also like to ask for tips on courses/books, preferably free or very cheap, so that I can learn more.

numero=int(input('Digite um numero: ')) div = 0 for x in range (1,numero+1):
    resto = numero % x
    if resto == 0:
        div += 1 if div == 2:
    print('{} é um numero primo.'.format(numero)) else:
    print('{} nao é um numero primo.'.format(numero)) escolha='a' escolha = input('deseja saber quais os numeros primos até {}? y=Sim / n=Não: '.format(numero)) while escolha != 'y' or escolha != 'n':
    escolha = input('Digite uma opcao valida. y=Sim / n=Não: '.format(numero)) if escolha == 'y':
    for num in range(numero+1):
        div=0
        for x in range(1,num+1):
            resto = num % x
            if resto == 0:
                div+=1
        if div==2:
                print(num) else:
    print('Até mais!!!')
  • Since you are asking for course tips, I recommend edx.org.here. It’s given by Microsoft, and it’s free. I started "from scratch" by this then there’s another one a little more advanced but still for beginners. If you want a course certificate you have to pay, see if the content fits what you are looking for :D

  • I’ll take a look, thanks for the :D indication

1 answer

0

The problem with your while is that you are passing two complementary conditions for execution. As you have already assigned 'a' to the choice variable previously, I recommend you replace while with

while True:
    escolha = input('Digite uma opcao valida. y=Sim / n=Não: '.format(numero))
    if escolha == 'n':
        break

As for courses, I personally recommend the Guanabara Python course at Video Course, it is playful and very well explained. I hope you like and good studies =]

  • I managed to solve using your solution, nor did I think to use while true, thank you!

Browser other questions tagged

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