When i run the code my “Elif” dont print anything at the terminal. The code just continues as if nothing is written inside the “elif” quote Im new to coding and i tried for so much time to correct this error but i just couldn’t :(
import time initialnumber = int(input("Insira o Numero inicial.n")) finalnumber= int(input("Insira o Numero Final.n")) while loop == True: counterinitial = initialnumber counterfinal = finalnumber print("Calculating...") print("tResults of the Square roots:") if (initialnumber < finalnumber): for quadrado in range (initialcounter,finalcounter+1,1): result = inicialnumber*inicialnumber print("t",inicialnumber,"-",result) numeroinicial = inicialnumber + 1 elif (inicialnumber> finalnumber): for quadrado in range (finalcounter,initialcounter-1,-1): result= finalnumber*finalnumber print("t",finalnumber,"-",result) finalnumber= finalnumber- 1 if initialnumber == finalnumber: result= initialnumber * initialnumber print("t",initialnumber,"-",resultado) loop = False time.sleep(5)
Advertisement
Answer
This is because the range is always empty and the for loop exits immediately:
elif (numeroinicial > numerofinal): for quadrado in range (contadorfinal,contadorinicial-1,-1): # ^^^ contadorfinal < contadorinicial as specified # in elif condition. # Can't count down from contadorfinal to contadorinicial. # Therefore this range will always be empty.
Should be:
elif (numeroinicial > numerofinal): for quadrado in range (contadorinicial,contadorfinal-1,-1):
with the two counters swapped. Like this, it works.