Jump to content
xisto Community
Sign in to follow this  
ROTNR

Python Just a *simple?* python question

Recommended Posts

Hey there guys/girls,

So, I have been working in Python for about a Month now, and have learned a lot. It seems similar to other programming languages I have used in the past (PHP, some C/C++). But I still have a few questions, so here is a sample of one of my codes.

##########Help #File#For #(Program name here)########import syshelp="""1. What is this?2. How does this work?3. What did you make this in?4. Back5. Quit"""print helphline = raw_input("Please select 1-5 above: ")while hline not in ('1','2','3','4','5'):    hline = raw_input("Please select a number between 1 and five: ")if hline == '1':    print("This is a program duh")while hline not in ('1','2','3','4','5'):    hline = raw_input("Please enter a number between 1 and 5: ")if hline == '2':    print("Through programming :-p")while hline not in ('1','2','3','4','5'):    hline = raw_input("Please enter a number between 1 and 5: ")if hline == '3':    print ("Python")while hline not in ('1','2','3','4','5'):    hline = raw_input("Please enter a number between 1 and 5: ")if hline == '4':    import index.pywhile hline not in ('1','2','3','4','5'):    hline = raw_input("Please enter a number between 1 and 5: ")if hline == 5:    sys.exit()
Okay, so I was wondering if there was an easier way to do this... I have tried elif , but it doesn't seem to work correctly. Also, whenever it brings me back to the start page and I go back to the help file and try to get back to the start page, it always just ends the program.

Thanks in advance,

-Pete

Edited by ROTNR (see edit history)

Share this post


Link to post
Share on other sites

The way you set up the program doesn't allow it to loop. You never clear the hline variable, therefore holding the value throughout the script, therefore passing by the while loops and the if statements. Assuming the following is what you want, this is the correct way of doing it:

#!/usr/bin/env pythonimport syshelp="""1. What is this?2. How does this work?3. What did you make this in?4. Back5. Quit"""print helpwhile True:	hline = raw_input("Please select a number between 1 and five: ")	if hline == '1':		print("This is a program duh")	elif hline == '2':		print("Through programming :-p")	elif hline == '3':		print("Python")	elif hline == '4':		import index.py	elif hline == '5':		sys.exit()

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this  

×
×
  • Create New...

Important Information

Terms of Use | Privacy Policy | Guidelines | We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.