Home > Software engineering >  how do i make my script return to the start or a specific line
how do i make my script return to the start or a specific line

Time:04-01

I am new to Python3. I have a condition in my code where if yes the the code should repeat from the beginning: if no i print a string and end the script. problem is i have the no part sorted but im struggling to make the code repeat.

here is the code:

#cardealer ascertains info about a car
#program will ask for model year and how many kms
#will include a condition weather to repeat or break

#dealer asks for model of car being sold
print("Hello, what make of car are you selling?",'n')
#seller inputs make
make= input(),'\n'

#Dealer asks what year the car is
print("What year is the car?",'\n')
#Seller input year of car
year= input()
print('\n')

#Dealer asks how much millage is on the clock
print("How much mileage does it have on the clock?",'\n')
#enter mileage
mileage= input()
print('\n')

#print all gathered information
print("Ok,your car is a", year, make,'\n' "with ",mileage  "km on 
the clock",2*'\n')

#ask seller if they have other cars to sell
print("Do you have any other cars you are willing to sell (enter 
Yes/No")
answer= input()
if answer=="No":2*'\n', print("OK! thank you I will get back in 
touch with you",exit())
if answer=="Yes":,line 5

i was wondering about the while true statement and get it to return but not sure how to implement it. or is that over course. all help appreciated and thank you in advance.

CodePudding user response:

If you want to use a while(True) statement, you have to put all of the code that you want to repeat inside the while loop and put a break in the end condition.

Your example could be something like:

while True:
    #dealer asks for model of car being sold
    print("Hello, what make of car are you selling?",'\n')
    #seller inputs make
    make= input(),'\n'

    #Dealer asks what year the car is
    print("What year is the car?",'\n')
    #Seller input year of car
    year= input()
    print('\n')

    #Dealer asks how much millage is on the clock
    print("How much mileage does it have on the clock?",'\n')
    #enter mileage
    mileage= input()
    print('\n')

    #print all gathered information
    print("Ok, your car is a", year, make,'\n' "with ",mileage  "km on the clock",2*'\n')

    #ask seller if they have other cars to sell
    print("Do you have any other cars you are willing to sell (enter Yes/No)")
    answer= input()

    if answer=="No":
        print("OK! Thank you I will get back in touch with you")
        break  # will exit the loop


    # if answer=="Yes":,line 5  # not needed

CodePudding user response:

Python does not have a go to line 5 like other languages such as vb.

It is common to use a while True in python for this (which one answer has already shown).

Alternatively, one can define a function and call it repeatedly as follows:

def main():
    ''' a function which calls itself under a certain condition '''

    answer = None
    #dealer asks for model of car being sold
    print("Hello, what make of car are you selling?",'n')
    #seller inputs make
    make= input(),'\n'

    #Dealer asks what year the car is
    print("What year is the car?",'\n')
    #Seller input year of car
    year= input()
    print('\n')

    #Dealer asks how much millage is on the clock
    print("How much mileage does it have on the clock?",'\n')

    #enter mileage
    mileage= input()
    print('\n')

    #print all gathered information
    print("Ok,your car is a", year, make,'\n' "with ",mileage  "km on the clock",2*'\n')

    #ask seller if they have other cars to sell
    print("Do you have any other cars you are willing to sell (enter Yes/No")
    answer= input()
    if answer=="No":2*'\n', print("OK! thank you I will get back in touch with you",exit())
    if answer=="Yes": main()  # <---  function calls itself if Yes.

# run the function
main()
  • Related