我仍然使用与之前相同的代码(我将发布更低版本),我只需要帮助将前10-15行转换为伪代码,这样我就可以自己了解如何做到这一点。
代码有效,缩进很奇怪,因为我很快就添加了空格来放入代码中。 对非专业代码道歉,我还是python的新手! 另外,请不要做整个伪代码,直到第一次真正的循环结束,所以我可以弄明白并自己完成剩下的工作。谢谢你:))
#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
答案 0 :(得分:1)
在伪代码中?我试着在Python上阅读一下,真的不难理解发生了什么。基本上:
import
导入此代码中所需的各种python模块
def restart_program():
定义了一个函数,您可以稍后在restart_program()
的代码中运行该函数
While True:
表示永远做某事,或直到break
。
.lower()
是字符串
如果您使用此代码作为基础,它不是特别pythonic,它使用类似旧的线性基本程序的样式,过度使用While True循环。编写同一个程序肯定有更短更可读的方式。