HiveBrain v1.2.0
Get Started
← Back to all entries
patternpythonMinor

Basic/beginner interactive program in Python

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
interactiveprogrambeginnerpythonbasic

Problem

I bring this code review plea over from suggestions from SO. I've made the suggested edits from that thread, except I kept one sys.exit() call because I was having trouble exiting otherwise. Overall, the code works as desired, and I'm looking for advice on how I can make it even cleaner.

```
import time
import sys

done = "I'm tired of you. Goodbye."
rainedOut = "Sorry, rain foiled your plans :("
dontUnderstand = "I'm sorry, I don't understand."

def good_weather():
"""Imagine a world where every 5 seconds it rains (good_weather = False),
then is sunny again (good_weather = True). This function should return
whether good_weather is True or False at the time it's called.
"""
return (time.time() % 10) <= 5

def start():
entries = 0

while entries < 4:
choice = raw_input("Hello! What do you want to do right now? Options: 1) Sleep, 2) Work, 3) Enjoy the great outdoors: ")

if choice == "1":
print "We are such stuff as dreams are made on, and our little life is rounded with a sleep. - Shakespeare, The Tempest"
elif choice == "2":
work()
elif choice == "3":
outdoors()
else:
print dontUnderstand
entries += 1
print done

def work():
entries = 0
entries2 = 0

while entries < 4:
choice = raw_input("Would you prefer sedentary office work or manual labor in the elements?: ")

if "office" in choice:
print "The brain is a wonderful organ; it starts working the moment you get up in the morning and does not stop until you get into the office. -Robert Frost"
start()
elif "manual" in choice:
sunny = good_weather()
if sunny == True:
print "A hand that's dirty with honest labor is fit to shake with any neighbor. -Proverb"
start()
else:
if entries2 < 3:
print rainedOut
entr

Solution

Ok here we go.

Style

You should get acquainted with the official python coding style, which is PEP8.
I notice especially naming violations with your global variables, which should follow the rules of functions: lowercase with underscore separation.

If-elsing

Perhaps it's just me, but I find heavily usage of if/elif/else messy.

Python does not support the typical switchstatement that could fit here, but there is an idiom that resembles it in python by using a dictionary of functions.

for example:

options = {1 : sleep,
           2 : work,
           3 : outdoors,
}
options[entries]()


this will require you to create an additional sleep function that can be executed to print the text We are such stuff as dreams are made on

This technique however lacks the break system that is available in switch statements, but you don't seem to need that in your code.

Additionally this scales really easily, all you got to do is add a new method, nicely encapsulated, and add it to the dictionary.

Multiple end points

Your code seems to have multiple end points. At the end of your start() function you print done. But also in your work() function you print it and terminate the program manually. It is cleaner to have a smooth main function, with multiple options (may it be function calls), when the function calls return, you simply print done only there and the program stops.

Main function

It is a good habit to use a main function, perhaps even better to rename your start to main and use the following construct:

def main():
    # Your main code

if __name__ == "__main__":
    main()


Goodluck!

Code Snippets

options = {1 : sleep,
           2 : work,
           3 : outdoors,
}
options[entries]()
def main():
    # Your main code

if __name__ == "__main__":
    main()

Context

StackExchange Code Review Q#98095, answer score: 4

Revisions (0)

No revisions yet.