我正在尝试找出如何将python代码转换为伪代码,使用其他代码似乎很容易,但我不确定自己的代码



我仍然使用与以前相同的代码(我将在下面发布),我只需要帮助将前 10-15 行转换为伪代码,所以我知道如何自己做。

代码

有效,缩进很奇怪,因为我快速添加了空格以放入代码中。抱歉非专业代码,我还是 python 新手!另外,请不要做整个伪代码,只是直到第一个 while true 循环的末尾,这样我就可以弄清楚并自己完成其余的工作。谢谢:)

#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 Namen")
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 Namen")
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 Numbern"))
        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 Namen")
        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 Cityn")
        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 Coden")
  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/yyyyn")
  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 forn"))
        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 programn")
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

在伪代码中?我会尝试阅读一些关于Python的内容,这真的不难理解发生了什么。基本上:

import导入此代码中所需的各种 python 模块。
def restart_program():定义了一个函数,稍后可以在代码中使用restart_program()
While True:意味着永远做某事,或者直到break
.lower()是字符串的小写版本

如果你使用这段代码作为基础,它并不是特别的pythonic,它使用了一种类似于旧的线性基本程序的风格,过度使用了While True循环。编写同一程序肯定有更短、更易读的方法。

相关内容

最新更新