patternpythonMinor
Small text-based game
Viewed 0 times
textgamesmallbased
Problem
I am brand new to coding in Python, and programming in general. I have picked up the Learning Python book and have been slowly making my way through it. I decided that I would try to implement what I have learned by writing a simple word-based game that I could run through the Mac OS Terminal.
I would love to hear any feedback on any segment of the code, or on the code as a whole! Please bear in mind that any suggestion that would be considered "advanced" would be lost on me at this point in my learning.
There might be a lot of irrelevant code. This practice was mostly to drill in what I have been learning.
```
###___def___ practice in game format
import time
def skip():
print
# Created to create adequate space after endgame()
def resskip():
print() * 10
#Used to repeat inputs if wrong input is selected
#Also used reset "inpot" value for choiceloop2() for sceneselect when
#an incorrect input is selected
def choiceerror2(option, frame):
inpot = option
if inpot == '1':
return choiceloop2(inpot, frame)
elif inpot == '2':
return choiceloop2(inpot, frame)
else:
print('Invalid option! Please try again!')
inpot = raw_input('What do you choose?: ')
return choiceerror2(inpot, frame)
#has the same functionality as choiceerror2(), scenes with 3 choices
def choiceerror3(option, frame):
inpot = option
if inpot == '1':
return choiceloop3(inpot, frame)
elif inpot == '2':
return choiceloop3(inpot, frame)
elif inpot == '3':
return choiceloop3(inpot, frame)
else:
print('Invalid option! Please try again!')
inpot = raw_input('What do you choose?: ')
return choiceerror3(inpot, frame)
#used primarily to assign a "scene" value for sceneselect
def choiceloop2(option, frame):
inpot = option
if inpot == '1':
return sceneselect(frame, '1')
else:
return sceneselect(frame, '2')
#used for the same thing as choi
I would love to hear any feedback on any segment of the code, or on the code as a whole! Please bear in mind that any suggestion that would be considered "advanced" would be lost on me at this point in my learning.
There might be a lot of irrelevant code. This practice was mostly to drill in what I have been learning.
```
###___def___ practice in game format
import time
def skip():
# Created to create adequate space after endgame()
def resskip():
print() * 10
#Used to repeat inputs if wrong input is selected
#Also used reset "inpot" value for choiceloop2() for sceneselect when
#an incorrect input is selected
def choiceerror2(option, frame):
inpot = option
if inpot == '1':
return choiceloop2(inpot, frame)
elif inpot == '2':
return choiceloop2(inpot, frame)
else:
print('Invalid option! Please try again!')
inpot = raw_input('What do you choose?: ')
return choiceerror2(inpot, frame)
#has the same functionality as choiceerror2(), scenes with 3 choices
def choiceerror3(option, frame):
inpot = option
if inpot == '1':
return choiceloop3(inpot, frame)
elif inpot == '2':
return choiceloop3(inpot, frame)
elif inpot == '3':
return choiceloop3(inpot, frame)
else:
print('Invalid option! Please try again!')
inpot = raw_input('What do you choose?: ')
return choiceerror3(inpot, frame)
#used primarily to assign a "scene" value for sceneselect
def choiceloop2(option, frame):
inpot = option
if inpot == '1':
return sceneselect(frame, '1')
else:
return sceneselect(frame, '2')
#used for the same thing as choi
Solution
You are using Python 2.x so
In
Don't use untransparent recursion. opening calls choiceloop calls sceneselect calls choiceerror calls choiceloop calls ...
Your code looks like a snake biting in her own tail. A programm should more look like a tree, go through branches to leafs and back the branches to other leafs:
Then you also don't have to get
Each of your
and your game would look like:
print is a statement, not a function, remove all parentheses from the prints or write in the first line of your codefrom __future__ import print_functionIn
reskip * 10 is not doing, what you think, it takes 10 times the elements of an empty tuple (e.g. nothing); the result is an empty tuple which is printed. You need a for-loop for your wanted result.Don't use untransparent recursion. opening calls choiceloop calls sceneselect calls choiceerror calls choiceloop calls ...
Your code looks like a snake biting in her own tail. A programm should more look like a tree, go through branches to leafs and back the branches to other leafs:
opening calls first scene, scene calls choiceloop, choiceloop returns a valid choice and scene returns this choice, so that opening can call the next scene.Then you also don't have to get
frame through all these functions.Each of your
choiceloops looks the same, you can write one general function:def choice(options):
print ', '.join('%d - %s' % (i, option) for i, option in enumerate(options, 1))
while True:
try:
answer = int(raw_input('Which way do you choose?: '))
if 1 <= answer <= len(options):
return options[answer - 1]
except ValueError:
pass
print('Invalid option! Please try again!')and your game would look like:
def scene_house():
print "nice room"
answer = choice(["street", "garden"])
if answer == "street":
print "you go through the front door"
return answer
def scene_garden():
print "nice trees"
answer = choice(["house"])
return answer
def scene_street():
print "car comes"
return "dead"
def main():
current_scene = "house"
while current_scene != "dead":
if current_scene == "house":
current_scene = scene_house()
if current_scene == "garden":
current_scene = scene_garden()
if current_scene == "street":
current_scene = scene_street()
print "you are dead"
if __name__ == '__main__':
main()Code Snippets
from __future__ import print_functiondef choice(options):
print ', '.join('%d - %s' % (i, option) for i, option in enumerate(options, 1))
while True:
try:
answer = int(raw_input('Which way do you choose?: '))
if 1 <= answer <= len(options):
return options[answer - 1]
except ValueError:
pass
print('Invalid option! Please try again!')def scene_house():
print "nice room"
answer = choice(["street", "garden"])
if answer == "street":
print "you go through the front door"
return answer
def scene_garden():
print "nice trees"
answer = choice(["house"])
return answer
def scene_street():
print "car comes"
return "dead"
def main():
current_scene = "house"
while current_scene != "dead":
if current_scene == "house":
current_scene = scene_house()
if current_scene == "garden":
current_scene = scene_garden()
if current_scene == "street":
current_scene = scene_street()
print "you are dead"
if __name__ == '__main__':
main()Context
StackExchange Code Review Q#115701, answer score: 6
Revisions (0)
No revisions yet.