Python Forum
Having issues getting a loop to work PLESSE HELP ASAP
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Having issues getting a loop to work PLESSE HELP ASAP
#1
hi people,

I'm trying to get a loop going for a project and just can't seem to get it work work. the program works before I put the 'while' statement in but as soon as I do the program does nothing. Ive included the program with the 'while' statement that stalls and also the program without


NOT WORKING PROGRAM
#Delcare 3 constants
StardardPolicy = 0.10
ExtraPremium = 0.05
GST = 0.10
#Delcare 2 start values
PolicySold = 0
TotalValue = 0
#Loop
keep_going = 'y'

while keep_going == 'y':

        def main ():
                Value_Of_Car = int(input( 'What is the Value of your Car?  '))
                Age_Of_Driver = int(input( 'What is the age of the driver?  '))
                Pay_By_Month = input( 'Would you like to pay by the month?  ')

                Cal_Premium (Value_Of_Car, Age_Of_Driver, Pay_By_Month)
        


        def Cal_Premium (num1, num2, num3):
                Pay_By_Month = num3
                Age_Of_Driver = num2
                Value_Of_Car = num1
                if Pay_By_Month == 'n':
                    print('Your Yearly Premium would be $', Value_Of_Car * StardardPolicy)
                else:
                    print('test test test')
        
        
    
                       


main ()
keep_going = input( 'Would you like to keep going? y/n  ')
WORKING NO LOOP
#Delcare 3 constants
StardardPolicy = 0.10
ExtraPremium = 0.05
GST = 0.10
#Delcare 2 start values
PolicySold = 0
TotalValue = 0
#Loop
keep_going = 'y'



def main ():
                Value_Of_Car = int(input( 'What is the Value of your Car?  '))
                Age_Of_Driver = int(input( 'What is the age of the driver?  '))
                Pay_By_Month = input( 'Would you like to pay by the month?  ')

                Cal_Premium (Value_Of_Car, Age_Of_Driver, Pay_By_Month)
        


def Cal_Premium (num1, num2, num3):
                Pay_By_Month = num3
                Age_Of_Driver = num2
                Value_Of_Car = num1
                if Pay_By_Month == 'n':
                    print('Your Yearly Premium would be $', Value_Of_Car * StardardPolicy)
                else:
                    print('test test')
        
        
    
                       


main ()
Reply
#2
It doesn't work the way you expect because inside the loop you define the functions (main() and Cal_premium(), and not call them.
You need to put the function definitions outside the loop and then just call main() in loop. But also your call to main() and keep_going input are not indented correctly, they are outside of while loop. And because the condition is always true (keep_going = 'y') the loop never exits.
So, put function definitions outside (before) while loop, and indent last two lines.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  While Loop Does Not Work Properly mactron 4 872 Jun-22-2023, 01:04 AM
Last Post: mactron
  For Loop Works Fine But Append For Pandas Doesn't Work knight2000 2 1,930 Dec-18-2021, 02:38 AM
Last Post: knight2000
  How can this for loop work without : ? Pedroski55 1 1,673 Dec-13-2020, 01:19 AM
Last Post: palladium
  Please help my while loop does not work as expected KingKhan248 6 2,562 Sep-28-2020, 09:12 PM
Last Post: deanhystad
  kill thread or process asap, even during time.sleep nanok66 4 2,868 Apr-29-2020, 10:13 AM
Last Post: nanok66
  For loop in my __init__ doesn't work as expected Jessy 2 2,320 Nov-18-2019, 10:07 AM
Last Post: buran
Question Why does modifying a list in a for loop not seem to work? umut3806 2 2,265 Jul-22-2019, 08:25 PM
Last Post: umut3806
  Need help asap lucaaa_s02 1 1,707 Jul-17-2019, 09:15 PM
Last Post: Larz60+
  Why doesn't my loop work correctly? (problem with a break statement) steckinreinhart619 2 3,156 Jun-11-2019, 10:02 AM
Last Post: steckinreinhart619
  for loop just work one Faruk 1 1,975 Jan-19-2019, 05:34 PM
Last Post: Larz60+

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020