Skip to main content
Code Review

Return to Answer

Commonmark migration
Source Link

DRY Principle

Gurkan Cetin has mentioned this. This code is a lot simpler than yours. A lot cleaner as well.

time_per_word=0.4
def display(text):
 print(text)
 sleep(len(text.split())*time_per_word)
story=["A few miles away,",
 ...
 "... and everything changed."
 ]
def story_mode():
 for x in story:
 display(x)
 

Even better is to store all the story lines in a text file and read it simply by using

story=open("story.txt").readlines()

Then you can supply each line to the display function as shown.

You can even have multiple stories at multiple times and load each with the same function. Do not make functions out of the program flow. Instead, make functions out of reusable parts of the program.

Learn and Use Simple Data Structures

#Learn and Use Simple Data Structures# AA tree is a perfect use of this type of general (question/answer/conditional next question) type of programs. Read about trees here. Implement a tree (python does not have an inbuilt type). Each node carries a question and responses to the question along with the parent's response. Then take all the children from a node and calculate the next question/node.

def get_next_node(present_question,answer_choices,chidrens_of_node):
 user_answer=get_input(present_question,answer_choices)
 for node in childrens_of_node:
 if node.parents_response==user_answer:
 return node
 
def get_input(input_prompt,input_options)
 lower_input_options=[input_option.lower() for input_option in input_options]
 user_input=input(input_prompt).lower() 
 while(user_input not in lower_input_options):
 user_input=input(input_prompt).lower()
 return user_input

Load your tree into memory using a text file as well.
These short reusable functions make your life a whole lot easier. Even if you have 10 answers to a given question giving 10 possible solutions it will still work. And the story can span across many nodes giving you thousands of different storylines.

The ready function is recursive. That is why you are having problems Do not write a recursive function when you don't need to and don't understand it. Read about recursion here. It is harder to implement recursion correctly even for very experienced programmers so keep that in mind.

DRY Principle

Gurkan Cetin has mentioned this. This code is a lot simpler than yours. A lot cleaner as well.

time_per_word=0.4
def display(text):
 print(text)
 sleep(len(text.split())*time_per_word)
story=["A few miles away,",
 ...
 "... and everything changed."
 ]
def story_mode():
 for x in story:
 display(x)
 

Even better is to store all the story lines in a text file and read it simply by using

story=open("story.txt").readlines()

Then you can supply each line to the display function as shown.

You can even have multiple stories at multiple times and load each with the same function. Do not make functions out of the program flow. Instead, make functions out of reusable parts of the program.

#Learn and Use Simple Data Structures# A tree is a perfect use of this type of general (question/answer/conditional next question) type of programs. Read about trees here. Implement a tree (python does not have an inbuilt type). Each node carries a question and responses to the question along with the parent's response. Then take all the children from a node and calculate the next question/node.

def get_next_node(present_question,answer_choices,chidrens_of_node):
 user_answer=get_input(present_question,answer_choices)
 for node in childrens_of_node:
 if node.parents_response==user_answer:
 return node
 
def get_input(input_prompt,input_options)
 lower_input_options=[input_option.lower() for input_option in input_options]
 user_input=input(input_prompt).lower() 
 while(user_input not in lower_input_options):
 user_input=input(input_prompt).lower()
 return user_input

Load your tree into memory using a text file as well.
These short reusable functions make your life a whole lot easier. Even if you have 10 answers to a given question giving 10 possible solutions it will still work. And the story can span across many nodes giving you thousands of different storylines.

The ready function is recursive. That is why you are having problems Do not write a recursive function when you don't need to and don't understand it. Read about recursion here. It is harder to implement recursion correctly even for very experienced programmers so keep that in mind.

DRY Principle

Gurkan Cetin has mentioned this. This code is a lot simpler than yours. A lot cleaner as well.

time_per_word=0.4
def display(text):
 print(text)
 sleep(len(text.split())*time_per_word)
story=["A few miles away,",
 ...
 "... and everything changed."
 ]
def story_mode():
 for x in story:
 display(x)
 

Even better is to store all the story lines in a text file and read it simply by using

story=open("story.txt").readlines()

Then you can supply each line to the display function as shown.

You can even have multiple stories at multiple times and load each with the same function. Do not make functions out of the program flow. Instead, make functions out of reusable parts of the program.

Learn and Use Simple Data Structures

A tree is a perfect use of this type of general (question/answer/conditional next question) type of programs. Read about trees here. Implement a tree (python does not have an inbuilt type). Each node carries a question and responses to the question along with the parent's response. Then take all the children from a node and calculate the next question/node.

def get_next_node(present_question,answer_choices,chidrens_of_node):
 user_answer=get_input(present_question,answer_choices)
 for node in childrens_of_node:
 if node.parents_response==user_answer:
 return node
 
def get_input(input_prompt,input_options)
 lower_input_options=[input_option.lower() for input_option in input_options]
 user_input=input(input_prompt).lower() 
 while(user_input not in lower_input_options):
 user_input=input(input_prompt).lower()
 return user_input

Load your tree into memory using a text file as well.
These short reusable functions make your life a whole lot easier. Even if you have 10 answers to a given question giving 10 possible solutions it will still work. And the story can span across many nodes giving you thousands of different storylines.

The ready function is recursive. That is why you are having problems Do not write a recursive function when you don't need to and don't understand it. Read about recursion here. It is harder to implement recursion correctly even for very experienced programmers so keep that in mind.

deleted 58 characters in body
Source Link
Gnik
  • 882
  • 1
  • 8
  • 19

DRY Principle

Gurkan Cetin has mentioned this. This code is a lot simpler than yours. A lot cleaner as well.

time_per_word=0.4
def display(text):
 print(text)
 sleep(len(text.split())*time_per_word)
story=["A few miles away,",
 ...
 "... and everything changed."
 ]
def story_mode():
 for x in story:
 display(x)
 

Even better is to store all the story lines in a text file and read it simply by using

story=open("story.txt").readlines()

Then you can supply each line to the display function as shown.

You can even have multiple stories at multiple times and load each with the same function. Do not make functions out of the program flow. Instead, make functions out of reusable parts of the program.

#Learn and Use Simple Data Structures# A tree is a perfect use of this type of general (question/answer/conditional next question) type of programs. Read about trees here. Implement a tree (python does not have an inbuilt type). Each node carries a question and responses to the question along with the parent's response; furthermore, each child contains possible next questionsresponse. Then take all the children from a node and calculate the next question/node.

def get_next_node(present_question,answer_choices,chidrens_of_node):
 user_answer=get_input(present_question,answer_choices)
 for node in childrens_of_node:
 if node.parents_response==user_answer:
 return node
 
def get_input(input_prompt,input_options)
 lower_input_options=[input_option.lower() for input_option in input_options]
 user_input=input(input_prompt).lower() 
 while(user_input not in lower_input_options):
 user_input=input(input_prompt).lower()
 return user_input

Load your tree into memory using a text file as well.
These short reusable functions make your life a whole lot easier. Even if you have 10 answers to a given question giving 10 possible solutions it will still work. And the story can span across many nodes giving you thousands of different storylines.

The ready function is recursive. That is why you are having problems Do not write a recursive function when you don't need to and don't understand it. Read about recursion here. It is harder to implement recursion correctly even for very experienced programmers so keep that in mind.

DRY Principle

Gurkan Cetin has mentioned this. This code is a lot simpler than yours. A lot cleaner as well.

time_per_word=0.4
def display(text):
 print(text)
 sleep(len(text.split())*time_per_word)
story=["A few miles away,",
 ...
 "... and everything changed."
 ]
def story_mode():
 for x in story:
 display(x)
 

Even better is to store all the story lines in a text file and read it simply by using

story=open("story.txt").readlines()

Then you can supply each line to the display function as shown.

You can even have multiple stories at multiple times and load each with the same function. Do not make functions out of the program flow. Instead, make functions out of reusable parts of the program.

#Learn and Use Simple Data Structures# A tree is a perfect use of this type of general (question/answer/conditional next question) type of programs. Read about trees here. Implement a tree (python does not have an inbuilt type). Each node carries a question and responses to the question along with the parent's response; furthermore, each child contains possible next questions. Then take all the children from a node and calculate the next question/node.

def get_next_node(present_question,answer_choices,chidrens_of_node):
 user_answer=get_input(present_question,answer_choices)
 for node in childrens_of_node:
 if node.parents_response==user_answer:
 return node
 
def get_input(input_prompt,input_options)
 lower_input_options=[input_option.lower() for input_option in input_options]
 user_input=input(input_prompt).lower() 
 while(user_input not in lower_input_options):
 user_input=input(input_prompt).lower()
 return user_input

Load your tree into memory using a text file as well.
These short reusable functions make your life a whole lot easier. Even if you have 10 answers to a given question giving 10 possible solutions it will still work. And the story can span across many nodes giving you thousands of different storylines.

The ready function is recursive. That is why you are having problems Do not write a recursive function when you don't need to and don't understand it. Read about recursion here. It is harder to implement recursion correctly even for very experienced programmers so keep that in mind.

DRY Principle

Gurkan Cetin has mentioned this. This code is a lot simpler than yours. A lot cleaner as well.

time_per_word=0.4
def display(text):
 print(text)
 sleep(len(text.split())*time_per_word)
story=["A few miles away,",
 ...
 "... and everything changed."
 ]
def story_mode():
 for x in story:
 display(x)
 

Even better is to store all the story lines in a text file and read it simply by using

story=open("story.txt").readlines()

Then you can supply each line to the display function as shown.

You can even have multiple stories at multiple times and load each with the same function. Do not make functions out of the program flow. Instead, make functions out of reusable parts of the program.

#Learn and Use Simple Data Structures# A tree is a perfect use of this type of general (question/answer/conditional next question) type of programs. Read about trees here. Implement a tree (python does not have an inbuilt type). Each node carries a question and responses to the question along with the parent's response. Then take all the children from a node and calculate the next question/node.

def get_next_node(present_question,answer_choices,chidrens_of_node):
 user_answer=get_input(present_question,answer_choices)
 for node in childrens_of_node:
 if node.parents_response==user_answer:
 return node
 
def get_input(input_prompt,input_options)
 lower_input_options=[input_option.lower() for input_option in input_options]
 user_input=input(input_prompt).lower() 
 while(user_input not in lower_input_options):
 user_input=input(input_prompt).lower()
 return user_input

Load your tree into memory using a text file as well.
These short reusable functions make your life a whole lot easier. Even if you have 10 answers to a given question giving 10 possible solutions it will still work. And the story can span across many nodes giving you thousands of different storylines.

The ready function is recursive. That is why you are having problems Do not write a recursive function when you don't need to and don't understand it. Read about recursion here. It is harder to implement recursion correctly even for very experienced programmers so keep that in mind.

Source Link
Gnik
  • 882
  • 1
  • 8
  • 19

DRY Principle

Gurkan Cetin has mentioned this. This code is a lot simpler than yours. A lot cleaner as well.

time_per_word=0.4
def display(text):
 print(text)
 sleep(len(text.split())*time_per_word)
story=["A few miles away,",
 ...
 "... and everything changed."
 ]
def story_mode():
 for x in story:
 display(x)
 

Even better is to store all the story lines in a text file and read it simply by using

story=open("story.txt").readlines()

Then you can supply each line to the display function as shown.

You can even have multiple stories at multiple times and load each with the same function. Do not make functions out of the program flow. Instead, make functions out of reusable parts of the program.

#Learn and Use Simple Data Structures# A tree is a perfect use of this type of general (question/answer/conditional next question) type of programs. Read about trees here. Implement a tree (python does not have an inbuilt type). Each node carries a question and responses to the question along with the parent's response; furthermore, each child contains possible next questions. Then take all the children from a node and calculate the next question/node.

def get_next_node(present_question,answer_choices,chidrens_of_node):
 user_answer=get_input(present_question,answer_choices)
 for node in childrens_of_node:
 if node.parents_response==user_answer:
 return node
 
def get_input(input_prompt,input_options)
 lower_input_options=[input_option.lower() for input_option in input_options]
 user_input=input(input_prompt).lower() 
 while(user_input not in lower_input_options):
 user_input=input(input_prompt).lower()
 return user_input

Load your tree into memory using a text file as well.
These short reusable functions make your life a whole lot easier. Even if you have 10 answers to a given question giving 10 possible solutions it will still work. And the story can span across many nodes giving you thousands of different storylines.

The ready function is recursive. That is why you are having problems Do not write a recursive function when you don't need to and don't understand it. Read about recursion here. It is harder to implement recursion correctly even for very experienced programmers so keep that in mind.

lang-py

AltStyle によって変換されたページ (->オリジナル) /