import os.path
#The model of the phone initialised to empty
strModel = ""
#Ask the user for their make
strModel = input("What is the make?")
#if the length of their input is too small ask them to input again
while len(strModel) <= 4:
#Promot the user to input again
strModel = input("Not enough characters. What is the make?")
#check if the file exists for specified phone
if os.path.isfile(strModel + ".txt") == True:
#open the file
f = open(strModel + ".txt", "r")
#turn contents of file into an array of strings for each line
lineArray = f.readlines()
#loop through the array to ask and answer questions
for line in lineArray:
#check if the line we are on is a question
if lineArray.index(line) % 2 == 0 or lineArray.index(line) == 0:
question = line
#ask the question and wait for reply
userAnswer = input(question)
#yes means the user has that problem so print the relevant answer
if(userAnswer == "yes"):
print(lineArray[lineArray.index(line)+1])
else:
print("Your model does not exist in our database.")
我是Python的初学者。我该如何为这段代码编写伪代码。这是向用户询问手机的代码。如果我能以什么方式进行,那么我对这段代码的效率会更高吗?
答案 0 :(得分:0)
我会告诉你我是怎么做到的;我这样做的主要原因是我不是编译器。另外......在编程课上我跳过那一周,因为我想编写&#34;真实代码&#34;。
通常,您编写伪代码来规划实际代码的编写。但是,像python IMO这样的语言并没有必要。
反正;我想到了算法或工作流程,无论我做什么;也许它是一个功能;如果它是一个函数,我会想到我需要执行该函数,然后快速表示它(伪代码)然后转换为实际代码。当然,我不是这样做的......除非是面试(因为,我不是编译/翻译/等);大多数面试官都不关心工作代码,他们希望看到你知道数据结构等。
对于你的代码,我在伪代码中会这样想:
strModel <= input
while strModel <= 4:
prompt
if path_is_valid:
fd = open file
for each line in file:
if this or that:
prompt user
if answer is yes:
do that