[Python] I am trying to find out how to convert python code to pseudocode, it seems easy with...

Discussão em 'Python' iniciado por Stack, Abril 3, 2025.

  1. Stack

    Stack Membro Participativo

    I am still working with the same code as before (I will post lower), I just need help converting the first 10-15 lines to pseudocode so I have an idea on how to do it myself.

    Code works, indents are weird because I quickly added spaces to put into code. Apologies for the non professional code, I am still new to python! Also, please don't do the whole of the pseudocode, just upto the end of the first while true loop so I can figure it out and do the rest myself. thank you :)

    #Taran Gill - S1714318

    #Please Open in Python and not IDLE

    #Code for car hire

    import sys
    import pc
    from datetime import date
    import time
    import os
    def restart_program():
    python = sys.executable
    os.execl(python, python, * sys.argv)

    #Opening Statement

    while True:
    opening = input("Welcome, would you like to hire a car? Please state
    'Yes' or 'No'\n")
    if opening.lower() == 'yes':
    print ("Thats good! First we need to take some details")
    break
    if opening.lower() == 'no':
    print ("Hope you have a good day!")
    print ("Exiting...")
    time.sleep(1)
    sys.exit()
    break
    else:
    print ("Invalid answer")
    print ("Restarting...")
    time.sleep(1)
    restart_program()
    continue

    #Asks user for First Name

    correct_name1 = False
    while correct_name1 == False:
    firstName = input("Please enter your First Name\n")
    if (firstName.replace (' ','').isalpha()==False):
    print("Not Acceptable")
    continue
    print ("Your First Name is", firstName.title())
    while True:
    correct1 = input("Is this correct?\n")
    if correct1.lower() == "yes":
    correct_name1 = True
    break
    if correct1.lower() == "no":
    correct_name1 = False
    break
    else:
    print ("Please say yes or no")

    #Asks user for Last Name

    correct_name2 = False
    while correct_name2 == False:
    lastName = input("Please enter your Last Name\n")
    if (lastName.replace (' ','').isalpha()==False):
    print("Not Acceptable")
    continue
    print ("Your Last Name is", lastName.title())
    while True:
    correct2 = input("Is this correct?\n")
    if correct2.lower() == "yes":
    correct_name2 = True
    break
    if correct2.lower() == "no":
    correct_name2 = False
    break
    else:
    print ("Please say yes or no")

    #Asks user for House Number

    correct_name3 = False
    while correct_name3 == False:
    try:
    houseNumber = int(input("Please enter your House Number\n"))
    if houseNumber < 1 or houseNumber > 2500:
    print ("Please put a number in between 1 and 2500")
    continue
    print ("Your House Number is", houseNumber)
    except ValueError:
    print ("Invalid Format")
    continue
    while True:
    correct3 = input("Is this correct?\n")
    if correct3.lower() == "yes":
    correct_name3 = True
    break
    if correct3.lower() == "no":
    correct_name3 = False
    break
    else:
    print ("Please say yes or no")

    #Asks user for Street Name

    correct_name4 = False
    while correct_name4 == False:
    try:
    streetName = input("Please enter your Street Name\n")
    if (len(streetName) <=4 or streetName.replace (' ','').isalpha()==False):
    print("Not Acceptable")
    continue
    print ("Your Street Name is", streetName.title())
    except ValueError:
    print ("Invalid Format")
    continue
    while True:
    correct4 = input("Is this correct?\n")
    if correct4.lower() == "yes":
    correct_name4 = True
    break
    if correct4.lower() == "no":
    correct_name4 = False
    break
    else:
    print ("Please say yes or no")

    #Asks user for City

    correct_name5 = False
    while correct_name5 == False:
    try:
    city = input("Please enter your City\n")
    if (len(city) <3 or city.replace (' ','').isalpha()==False):
    print("Not Acceptable")
    continue
    print("Your City is",city.title())
    except ValueError:
    print ("Invalid Format")
    continue
    while True:
    correct5 = input("Is this correct?\n")
    if correct5.lower() == "yes":
    correct_name5 = True
    break
    if correct5.lower() == "no":
    correct_name5 = False
    break
    else:
    print ("Please say yes or no")


    #Asks user for Post Code

    correct_name6 = False
    while correct_name6 == False:
    postcode = input ("Please enter your Post Code\n")
    try:
    checkpc = pc.parse_uk_postcode(postcode)
    except ValueError:
    print ("Invalid Post Code, Please try again")
    continue
    else:
    print ("Your Post Code is", postcode.upper())
    while True:
    correct6 = input("Is this correct?\n")
    if correct6.lower() == "yes":
    correct_name6 = True
    break
    if correct6.lower() == "no":
    correct_name6 = False
    break
    else:
    print ("Please say yes or no")

    #Asks user for Date Of Hire

    correct_name7 = False
    while correct_name7 == False:
    dateOfHire = input("Please enter the date of start of hire in the format - dd/mm/yyyy\n")
    try:
    valid_date = time.strptime(dateOfHire, '%d/%m/%Y')
    except ValueError:
    print("Invalid format, please try again")
    continue
    else:
    print("The date of start of hire is", dateOfHire)
    while True:
    correct7 = input("Is this correct?\n")
    if correct7.lower() == "yes":
    correct_name7 = True
    break
    if correct7.lower() == "no":
    correct_name7 = False
    break
    else:
    print ("Please say yes or no")

    #Asks the user for the Amount of Days they would like to hire the car for

    correct_name8 = False
    while correct_name8 == False:
    try:
    amountOfDays = int(input("Please enter the amount of days you would like to hire for\n"))
    if amountOfDays <1 or amountOfDays > 100:
    print ("Please enter a sensible number!")
    continue
    except ValueError:
    print ("Invalid Format")
    continue
    else:
    print ("The amount of days you would like to hire the vehicle is", amountOfDays, "days")
    while True:
    correct8 = input("Is this correct?\n")
    if correct8.lower() == "yes":
    correct_name8 = True
    break
    if correct8.lower() == "no":
    correct_name8 = False
    break
    else:
    print ("Please say yes or no")

    #Tells the user what the prices are for each vehicle

    print ("""
    Estate costs 50 pounds a day
    Saloon costs 60 pounds a day
    Sports costs 70 pounds a day
    There will be an additional 10 pound charge for each day
    if the estimated miles is above 100 miles
    """)

    #Asks the user what vehicle they want

    correct_name9 = False
    while correct_name9 == False:
    car = input("Would you like an Estate, Saloon or Sports?\n")
    if car.lower() in ["estate", "saloon", "sports"]:
    print("The car you have chosen is", car.title())
    else:
    print ("Invalid car, please try again")
    continue
    while True:
    correct9 = input("Is this correct?\n")
    if correct9.lower() == "yes":
    correct_name9 = True
    break
    if correct9.lower() == "no":
    correct_name9 = False
    break
    else:
    print ("Please say yes or no")

    #Asks the user how many miles they think they will do in a day

    correct_name10 = False
    while correct_name10 == False:
    try:
    estMiles = int(input("How many miles do you think you will do in a day?\n"))
    if estMiles < 1 or estMiles > 1500:
    print ("Please enter a sensible number!")
    continue
    except ValueError:
    print ("Invalid Format")
    continue
    else:
    print ("The amount of miles you think you will do in a day is", estMiles)
    while True:
    correct10 = input("Is this correct?\n")
    if correct10.lower() == "yes":
    correct_name10 = True
    break
    if correct10.lower() == "no":
    correct_name10 = False
    break
    else:
    print ("Please say yes or no")

    #Calculations for each vehicle

    if car == "estate":
    calculation = 50 * amountOfDays
    elif car == "saloon":
    calculation = 60 * amountOfDays
    elif car == "sports":
    calculation = 70 * amountOfDays

    if estMiles > 100:
    calculation = calculation + (10*amountOfDays)

    print ("It will cost you", calculation, "pounds.")

    #Lets the user decide if they want to input another vehicle or exit

    while True:
    answer = input("Type restart to start over, type exit to exit program\n")
    if answer.lower() == 'restart':
    print ("Restarting...")
    time.sleep(1)
    restart_program()
    elif answer.lower() == 'exit':
    print ("Exiting...")
    time.sleep(1)
    sys.exit
    break
    else:
    print ("Invalid Format, Please try again")
    continue

    Continue reading...

Compartilhe esta Página