How to create a menu or directory? [closed] - python

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 8 years ago.
This question appears to be off-topic because it lacks sufficient information to diagnose the problem. Describe your problem in more detail or include a minimal example in the question itself.
Questions must demonstrate a minimal understanding of the problem being solved. Tell us what you've tried to do, why it didn't work, and how it should work. See also: Stack Overflow question checklist
Improve this question
How do I make a simple Menu/Directory using Python? I would like to have letters that the user would press to do tasks, and when they enter the letter after the prompt, the task is done... for example:
A. Create Username
B. Edit Username
C. Exit
Choice:
And then all the user has to do is enter one of the letters after the prompt.

A (very) basic approach would be something like this:
print "A. Create Username"
print "B. Edit Username"
input = raw_input("Enter your choice")
if input == "A":
print "A was given"
if input == "B":
print "B was given"

A very basic version:
def foo():
print "Creating username..."
def bar():
print "Editing username..."
while True:
print "A. Create Username"
print "B. Edit Username"
print "C. Exit"
choice = raw_input()
if choice.lower() == 'a':
foo()
elif choice.lower() == 'b':
bar()
elif choice.lower() == 'c':
break
else:
print "Invalid choice"
Accepts upper- and lower-case letters as choice.

Console Menu Generator in Python
Have a read and post back with your efforts as stated by dm03514

Related

Test case for the magic-8-ball game [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 4 years ago.
Improve this question
I'm mew to python and to TDD in general, I have developed a magic-8-ball game and I would like to learn how I can write the Test case for this program. The test should ensure the following:
Allow the user to input their question
Show an in progress message
Create 10/20 responses and show a random response
Allow the user to ask another question/advice or quit the game.
Below is my code. I know I should write Tests first but like I said, this a new territory for me.
RESPONSES = ("It is certain", "It is decidedly so", "Without a doubt", "Yes-definitely",
"You may rely on it", "As I see it, yes", "Most likely", "Outlook good", "Yes",
"Signs point to yes", "Reply is hazy", "Ask again later", "Better not tell you now",
"Cannot predict now", "Concentrate and ask again", "Don't count on it",
" My reply is no", "My sources say no", "Outlook not so good", "Very doubtful")
from time import sleep
from random import choice
class MagicBall:
def input_question(self):
play_again = 'yes'
while play_again == 'yes':
str(input('Enter your question: '))
for i in range(3):
print("Loading {}".format(".."*i))
sleep(1)
print(choice(RESPONSES))
play_again = str(input("Would you like to ask another question? yes/no ")).lower()
if play_again == 'no':
print("Goodbye! Thanks for playing!")
SystemExit()
magic = MagicBall()
magic.input_question()
Unit tests are written to confirm that the expected output is obtained from a range of inputs for any function or method that carries out some computations and returns a value.
If you had a function to calculate the sum of two number:
def calc(first,second):
return first + second
To confirm that the correct result is obtained you would do the following:
self.assertEqual(calc(5,5), 10)
You expect 10 to be the result from 5 and 5 so if it is something else it will produce an error.
I have also noticed this is a question from the Andela Interview questions scheduled for next week. Please review the documentation you were given for more clarity on how to write tests for different cases.

Indent placement? [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 8 years ago.
Improve this question
I've tried to design a small (3 rooms) "adventure-like" mini-game, and when i run it i get:
if answer == "restaurant":
IndentationError: unindent does not match any outer indentation level
# TRUMANIA v. 0.0.2
def Square():
print "You exit from there, and you appear in Central Square. Do you want to walk to the Library or to the Restaurant?"
answer = raw_input("Type 'Restaurant' or 'Library' and hit 'Enter'.").lower()
if answer == "restaurant":
Restaurant()
elif answer == "library":
Library()
else:
print "You didn't pick Restaurant or Library! You can't just stand there like a rock; you need to make a decision!"
Square()
def Trumania():
print "You've just entered Trumania!"
print "Do you want to go to the Restaurant or visit the Library?"
answer = raw_input("Type 'Restaurant' or 'Library' and hit 'Enter'.").lower()
if answer == "restaurant":
Restaurant()
elif answer == "library":
Library()
else:
print "You didn't pick Restaurant or Library! You can't just stand in the entrance like a scarecrow; you need to make a decision!"
Trumania()
def Restaurant():
print "You've just entered the Restaurant!"
print "Do you want to eat Inside or Outside?"
answer = raw_input("Type 'Inside' or 'Outside' and hit 'Enter'.").lower()
if answer == "inside":
print "You need to bribe the waiter, but you get a cozy table and have a wonderful meal!"
elif answer == "outside":
print "You get a cheap (and cold) table in the outside and manage to eat some apetizers"
else:
print "You didn't selected Inside or Outside. The Waiter euh... Waits... "
Restaurant()
def Library():
print "You arrive at the Library!"
print "Do you want to get some books or you prefer to study?"
answer = raw_input("Type 'Books' or 'Study' and hit 'Enter'.").lower()
if answer == "books":
print "You get some books that you can sell later. However, thats bad karma!"
elif answer == "study":
print "You learn a lot!However, you feel tired after so much effort"
else:
print "You didn't pick Books or Study! Try again."
Library()
Trumania()
Square()
The idea is to start in Trumannia, then be able to choose Restaurant or Library, and then exit to Square and be able to choose again Restaurant or Library. So I'm not sure a) how to fix the error and b) how to format the different variables so the program can "load" all the info first and then "go" to each place when needed. I hope i explained myself. Thanks again!
Your code is broken because there is no colon after def Square().

Python programming error, elif statment syntax error Simple code [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 8 years ago.
Improve this question
name=input("Hello person, Whats your name?")
print("Hello", name)
print("Do you want to hear a story?", name)
choice=input("Yes, No?")
if choice==("yes" or "yes " or "Yes" or "Yes "):
print("Ok", name,", listen up")
print("There was once an old, old house at the top of a hill Sooooo high it was above the clouds")
choice2=input("What do you want to call the house?")
print("The old,",choice2,"was once owned by an old lady. ")
elif choice==("maybe"):
print("You found an easter egg, congrats. PS this does nothing")
Whats wrong with this code?? It says in the idle shell syntax error. The last elif statement isn't working.
This is a petty indentation issue, your print statements for the if blocks are not indented right and so the elif seems to be out of place. Note that python keeps track of logical blocks by the indentation.
name=input("Hello person, Whats your name?")
print("Hello", name)
print("Do you want to hear a story?", name)
choice=input("Yes, No?")
if choice==("yes" or "yes " or "Yes" or "Yes "):
print("Ok", name,", listen up")
print("There was once an old, old house at the top of a hill Sooooo high it was above the clouds")
choice2=input("What do you want to call the house?")
print("The old,",choice2,"was once owned by an old lady. ")
elif choice==("maybe"):
print("You found an easter egg, congrats. PS this does nothing")
As already pointed out, if choice==("yes" or "yes " or "Yes" or "Yes ") is wrong, use if choice.lower().strip() == "yes" instead, or if choice in ("yes", "yes ", "Yes", "Yes ").
If in case this is python 2, input will throw an error, use raw_input instead.
Also print with multiple statements will throw errors as well if used like a function, so change them from print(statement_x, statement_y, statement_z) to print statement_x, statement_y, statement_z

Can only input one thing in Python [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I'm having a problem where I want the user to be able to input text to call functions. It works perfectly fine except for one thing. :/ Once something has been input nothing can be done afterwards. The only way to solve it is run the program again which is not convenient. I have spent a lot of time looking for an answer and need help. I also think other amateurs like me might be wondering this too.
An example of the code:
x = raw_input('test1')
if x == 'x':
print 'test2'
The result:
test1x
test2
x
'x'
As you can see it works once then stops working. For the record I'm using Python 2. Hope this can be solved :)
You need to use a loop if you want to program to keep running.
Here is a simple example:
while True:
n = raw_input("Please enter 'hello':")
if n.strip() == 'hello':
break
The program will keep running until you type hello
You can use the following function
def call():
input = raw_input('input: ')
if input == 'yes':
print 'yes'
call()
call()
last_data = ''
while last_data != 'yes':
input = raw_input('ENTER SOMETHING: ')
#do whatever you want with input
last_data = raw_input('DO YOU WANT TO QUIT? (yes/no): ')

How to start a program Automatically from the main method [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 6 years ago.
Improve this question
Here is my code
def main():
# This code reads in data.txt and loads it into an array
# Array will be used to add friends, remove and list
# when we quit, we'll overwrite original friends.txt with
# contents
print"Welcome to the program"
print "Enter the correct number"
print "Hockey fan 1, basketball fan 2, cricket fan 3"
choice = input("Select an option")
while choice!=3:
if choice==1:
addString = raw_input("Who is your favorite player??")
print "I love Kessel"
elif choice==2:
remInt = raw_input("Do you think that the Cavaliers will continue ther loosing ways?")
print "I think they can beat the Clippers"
else:
"You must choose a Number (1,2 or 3)"
print "Cricket is a great sport"
choice = input("Select an option")
inFile = open('data.txt','r')
listNumbers = []
for numbers in inFile:
listNumbers.append(numbers)
print numbers
inFile.close()
if __name__ == "__main__":
main() # will call the 'main' function only when you execute this from the command line.
Add:
if __name__ == "__main__":
main()
To your script (indented all the way to the left; not as part of the main() function).
Add:
if __name__ == "__main__":
main()
to the end of the file
You should try the following
if __name__ == "__main__":
main()
Once that is done, you should call your program as follows from the command line (assuming you are on Linux/Mac)
python <your_prog>
Also it will be helpful if you give the exact error that you are getting.
Also make sure that all the indentation is correct.

Categories

Resources